-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
81 lines (70 loc) · 2.2 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package huffmancoding;
/**
* Bare bones BinaryTree, suitable only for this Huffman implementation. Does
* not feature all standard methods of a typical Binary Tree.
*/
public class BinaryTree {
//If this tree is empty or not
private boolean ifEmpty;
//Root node of this tree
private Node rootNode;
/* Left and right child sub-trees;
* Uses entire Binary trees for children (instead of just nodes) for ease of
* recursive traversal in our encoding algorithm
*/
private BinaryTree leftChild, rightChild;
/**
* Empty constructor to build a Binary tree with no elements.
*/
public BinaryTree() {
this.ifEmpty = true;
}
/**
* Constructor for a Binary tree using a given root Node and left and right
* children (trees).
* @param root - Node
* @param leftChild - BinaryTree
* @param rightChild - BinaryTree
*/
public BinaryTree(Node root, BinaryTree leftChild, BinaryTree rightChild)
throws NullPointerException {
this.ifEmpty = false;
this.rootNode = root;
this.leftChild = (leftChild == null) ? new BinaryTree() : leftChild;
this.rightChild = (rightChild == null) ? new BinaryTree() : rightChild;
}
//////////////////////////////ACCESSORS/////////////////////////////////////
/**
* Returns if this tree is empty.
* @return ifEmpty
*/
public boolean isEmpty() {
return this.ifEmpty;
}
/**
* Returns the root of this tree.
* @return root node
*/
public Node getRoot() {
if (ifEmpty) return null;
else return this.rootNode;
}
/**
* Returns the left sub-tree of this tree, including all children of said
* child.
* @return left child tree
*/
public BinaryTree getLeftChild() {
if (ifEmpty) return null;
else return leftChild;
}
/**
* Returns the right sub-tree of this tree, including all children of said
* child.
* @return right child tree
*/
public BinaryTree getRightChild() {
if (ifEmpty) return null;
else return rightChild;
}
}