Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 13ca958

Browse files
committed
Fixed some minor issues
1 parent 2fab9ef commit 13ca958

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/main/java/com/rampatra/common/BinaryTree.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import static java.lang.System.out;
66

77
/**
8-
* Created by IntelliJ IDEA.
8+
* Basic binary tree functions like put, delete, height, traversals, etc.
99
*
10-
* @author: ramswaroop
11-
* @date: 4/19/15
12-
* @time: 6:35 PM
13-
* @see: https://www.cs.bu.edu/teaching/c/tree/breadth-first/
10+
* @author rampatra
11+
* @since 4/19/15
12+
* @link https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html
13+
* @link http://typeocaml.com/2014/11/26/height-depth-and-level-of-a-tree/
1414
*/
1515
public class BinaryTree<E extends Comparable<E>> extends Tree<E> {
1616

@@ -46,10 +46,12 @@ public BinaryNode<E> put(BinaryNode<E> node, E value) {
4646
return node;
4747
}
4848

49-
/**
50-
* Traversals.
51-
*/
5249

50+
/***********************************
51+
*
52+
* Tree Traversals.
53+
*
54+
***********************************/
5355

5456
/**
5557
* Prints the pre-order traversal of the tree.
@@ -164,25 +166,28 @@ public void deleteChildren(BinaryNode<E> node) {
164166

165167

166168
/**
167-
* Return the height of the tree.
169+
* Height of the tree is the number of edges from the root to its farthest leaf.
170+
* Note: The height of binary tree with single node is taken as zero.
168171
*
169-
* @return
172+
* @return the height of the tree.
170173
*/
171174
public int height() {
172175
return height(root);
173176
}
174177

175178
public int height(BinaryNode<E> node) {
176-
if (node == null) return 0;
179+
if (node == null || (node.left == null && node.right == null)) {
180+
return 0;
181+
}
177182

178183
return Math.max(height(node.left), height(node.right)) + 1;
179184
}
180185

181186

182187
/**
183-
* Returns the number of nodes currently in the tree.
188+
* Size of tree.
184189
*
185-
* @return
190+
* @return the number of nodes currently in the tree.
186191
*/
187192
public int size() {
188193
return size(root);
@@ -266,8 +271,15 @@ public static void main(String[] a) {
266271
bt.put(6);
267272
bt.put(7);
268273
bt.put(8);
274+
275+
out.print("BFS: ");
269276
bt.breadthFirstTraversal();
270-
out.println();
277+
out.print("\nPre Order: ");
278+
bt.preOrder();
279+
out.print("\nIn Order: ");
271280
bt.inOrder();
281+
out.print("\nPost Order: ");
282+
bt.postOrder();
283+
out.println("\nHeight of tree: " + bt.height());
272284
}
273285
}

0 commit comments

Comments
 (0)