Skip to content

Commit c36665d

Browse files
author
Ram swaroop
committed
code refactoring
1 parent 045d288 commit c36665d

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

src/me/ramswaroop/common/BinaryTree.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
/**
66
* Created by IntelliJ IDEA.
7-
* User: ramswaroop
8-
* Date: 4/19/15
9-
* Time: 6:35 PM
10-
* To change this template go to Preferences | IDE Settings | File and Code Templates
7+
*
8+
* @author: ramswaroop
9+
* @date: 4/19/15
10+
* @time: 6:35 PM
11+
* @see: https://www.cs.bu.edu/teaching/c/tree/breadth-first/
1112
*/
1213
public class BinaryTree<E extends Comparable<E>> extends Tree<E> {
1314

@@ -151,7 +152,7 @@ public void delete(E value) {
151152
*
152153
* @param node
153154
*/
154-
public void deleteChildrens(BinaryNode<E> node) {
155+
public void deleteChildren(BinaryNode<E> node) {
155156
if (node == null) {
156157
return;
157158
}
@@ -238,9 +239,10 @@ public int width(BinaryNode<E> node, int width) {
238239

239240
if (node.left == null && node.right == null) return 1; // for single/leaf node
240241

241-
int level_width = width(node.left, width) + width(node.right, width);
242+
int levelWidth = width(node.left, width) + width(node.right, width);
242243

243-
if (level_width > width) width = level_width;
244+
// to find max width
245+
if (levelWidth > width) width = levelWidth;
244246

245247
return width;
246248
}
@@ -250,4 +252,15 @@ public void printValue(BinaryNode<E> node) {
250252

251253
out.print(node.value);
252254
}
255+
256+
// test cases
257+
public static void main(String[] a) {
258+
BinaryTree<Integer> bt = new BinaryTree<>();
259+
bt.put(1);
260+
bt.put(2);
261+
bt.put(3);
262+
bt.put(4);
263+
bt.put(5);
264+
bt.breadthFirstTraversal();
265+
}
253266
}

src/me/ramswaroop/trees/CheckForBST.java

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.ramswaroop.trees;
22

33
import me.ramswaroop.common.BinaryNode;
4+
import me.ramswaroop.common.BinarySearchTree;
45
import me.ramswaroop.common.BinaryTree;
56

67
import java.util.List;
@@ -13,22 +14,11 @@
1314
* @author: ramswaroop
1415
* @date: 6/26/15
1516
* @time: 7:14 PM
17+
*
18+
* Concept: Perform in-order traversal of the tree and if
19+
* the result isn't in ascending order then returns false.
1620
*/
17-
public class CheckForBST<E extends Comparable<E>> extends BinaryTree<E> {
18-
19-
/**
20-
* Checks whether the binary tree is a BST or not.
21-
* <p/>
22-
* Approach: Performs in-order traversal of the tree and if
23-
* the result isn't in ascending order then returns false.
24-
*
25-
* @return
26-
*/
27-
public boolean isBST() {
28-
//List<BinaryNode<E>> list = new ArrayList<>();
29-
BinaryNode<E> prev = new BinaryNode<>(null);
30-
return isBST(root, prev);
31-
}
21+
public class CheckForBST {
3222

3323
/**
3424
* Traverse the tree in in-order fashion and insert all nodes
@@ -38,7 +28,7 @@ public boolean isBST() {
3828
* @param list
3929
* @return
4030
*/
41-
public boolean isBST(BinaryNode<E> node, List<BinaryNode<E>> list) {
31+
public static <E extends Comparable<E>> boolean isBST(BinaryNode<E> node, List<BinaryNode<E>> list) {
4232
if (node == null) return true;
4333

4434
boolean left = isBST(node.left, list);
@@ -62,7 +52,7 @@ public boolean isBST(BinaryNode<E> node, List<BinaryNode<E>> list) {
6252
* @param prev
6353
* @return
6454
*/
65-
public boolean isBST(BinaryNode<E> node, BinaryNode<E> prev) {
55+
public static <E extends Comparable<E>> boolean isBST(BinaryNode<E> node, BinaryNode<E> prev) {
6656
if (node == null) return true;
6757

6858
boolean left = isBST(node.left, prev);
@@ -79,14 +69,23 @@ public boolean isBST(BinaryNode<E> node, BinaryNode<E> prev) {
7969
}
8070

8171
public static void main(String a[]) {
82-
CheckForBST<Integer> bt = new CheckForBST<>();
83-
bt.put(6);
84-
bt.put(3);
85-
bt.put(5);
86-
bt.put(7);
87-
bt.put(8);
88-
bt.put(9);
72+
BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
73+
binarySearchTree.put(6);
74+
binarySearchTree.put(3);
75+
binarySearchTree.put(5);
76+
binarySearchTree.put(7);
77+
binarySearchTree.put(8);
78+
binarySearchTree.put(9);
79+
out.println("Is BST: ");
80+
out.println(isBST(binarySearchTree.root, new BinaryNode<Integer>(null)));
81+
BinaryTree<Integer> binaryTree = new BinaryTree<>();
82+
binaryTree.put(6);
83+
binaryTree.put(3);
84+
binaryTree.put(5);
85+
binaryTree.put(7);
86+
binaryTree.put(8);
87+
binaryTree.put(9);
8988
out.println("Is BST: ");
90-
out.println(bt.isBST());
89+
out.println(isBST(binaryTree.root, new BinaryNode<Integer>(null)));
9190
}
9291
}

0 commit comments

Comments
 (0)