-
Notifications
You must be signed in to change notification settings - Fork 23
/
BinaryTree.java
50 lines (38 loc) · 1.26 KB
/
BinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
abstract class BinaryTree {
protected TreeNode root;
/* While this constructor cannot be called directly,
it is useful because the child classes can use it,
by using super(), to instantiate the root node
*/
public BinaryTree(int rootValue) {
this.root = new TreeNode(rootValue);
}
public void inOrderTraversal() {
//TODO
}
public void preOrderTraversal() {
//TODO
}
public void postOrderTraversal() {
//TODO
}
/*NOTE: Think about why these methods are abstract.
Why can't we just implement them like we did for the traversals?
*/
/* Insert a node with the given value into the tree
Return true if the insert succeeds, else return false.
This is a signature similar to the one we saw in the CFW.
*/
public abstract boolean insert(int value);
/* Delete the node with the given value from this tree, and
return the deleted node. Return null if the value doesn't exist.
*/
public abstract TreeNode delete(int value);
/* Return true if the given value exists in the tree
*/
public abstract boolean exists(int find);
/* Return the node that is the LCA of the two given values.
Return null if either or both of the values doesn't exist in the tree.
*/
public abstract TreeNode lowestCommonAncestor(int value1, int value2);
}