-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.java
136 lines (125 loc) · 3.04 KB
/
TreeNode.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ds;
/*
a tree node extends a node, and has a left and right child in addition
to the inherited values and other elements of Node
*/
class TreeNodeException extends Exception {
public TreeNodeException(String msg) {
super(msg);
}
}
public class TreeNode extends Node {
protected TreeNode left;
protected TreeNode right;
protected int count; // the number of times this value has been inserted
public TreeNode(int n) {
super(n);
this.count = 1;
left = null;
right = null;
}
protected void setLeft(TreeNode n) {
this.left = n;
}
protected void setRight(TreeNode n) {
this.right = n;
}
/*
AVL tree; calculate balance factor
*/
protected int balanceFactor(TreeNode n) {
int leftSubtree = height(n.left);
int rightSubtree = height(n.right);
return leftSubtree - rightSubtree;
}
protected boolean balancedAt(TreeNode n) {
int b = balanceFactor(n);
if (b <= 1 || b >= -1) {
return true;
}
return false;
}
// calculate max height of two sides
protected int height(TreeNode n) {
// recursion
int height = 1;
if (n.right == null && n.left == null) {
return height;
}
else if (n.right == null && n.left != null) {
height += height(n.left);
}
else if (n.right != null && n.left == null) {
height += height(n.right);
}
else { // right and left are null
int right = height(n.right);
int left = height(n.left);
if (right > left) {
height += right;
}
else {
height += left;
}
}
return height;
}
public boolean isLeaf() {
if (this.left == null && this.right == null) {
return true;
}
return false;
}
public boolean trueIf(int v) {
if (this.value == v) {
return true;
}
else if (this.value > v && this.left.trueIf(v)) {
return true;
}
else if (this.value < v && this.right.trueIf(v)) {
return true;
}
return false;
}
public int findCount(int v) throws TreeNodeException {
if (this.value == v) {
return this.count;
}
else if (this.value > v && this.left == null) {
return 0;
}
else if (this.value > v && this.left != null) {
return this.left.findCount(v);
}
else if (this.value < v && this.right == null) {
return 0;
}
else if (this.value < v && this.right != null) {
return this.right.findCount(v);
}
else {
throw new TreeNodeException("broken tree");
}
}
public void insert(int v) throws TreeNodeException {
if (this.value == v) {
this.count++;
}
else if (this.value > v && this.left == null) {
this.left = new TreeNode(v);
}
else if (this.value > v && this.left != null) {
this.left.insert(v);
}
else if (this.value < v && this.right == null) {
this.right = new TreeNode(v);
}
else if (this.value < v && this.right != null) {
this.right.insert(v);
}
else {
throw new TreeNodeException("broken tree");
}
}
}