-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.java
More file actions
189 lines (163 loc) · 3.32 KB
/
Copy pathTreeNode.java
File metadata and controls
189 lines (163 loc) · 3.32 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
public class TreeNode<T>
{
private TreeNode<T> rightChild;
private TreeNode<T> leftChild;
private TreeNode<T> parent;
T self;
public TreeNode(T self)
{
this.self = self;
}
public void deleteNode(T search)
{
if (search.equals(self))
{
if (leftChild != null && rightChild != null)
{
throw new UnsupportedOperationException("Cannot remove a node with two children!");
}
else if (rightChild == null)
{
if (parent.getLeftChild() == this)
{
if (leftChild == null)
parent.setLeftChild(null);
else
{
leftChild.setParent(parent);
parent.setLeftChild(leftChild);
}
}
else if (parent.getRightChild() == this)
{
if (leftChild == null)
parent.setRightChild(null);
else
{
leftChild.setParent(parent);
parent.setRightChild(leftChild);
}
}
}
else if (leftChild == null)
{
if (parent.getLeftChild() == this)
{
if (rightChild == null)
parent.setLeftChild(null);
else
{
rightChild.setParent(parent);
parent.setLeftChild(rightChild);
}
}
else if (parent.getRightChild() == this)
{
if (rightChild == null)
parent.setRightChild(null);
else
{
rightChild.setParent(parent);
parent.setRightChild(rightChild);
}
}
}
return;
}
if (leftChild != null)
leftChild.deleteNode(search);
if (rightChild != null)
rightChild.deleteNode(search);
}
public TreeNode<T> get(T search)
{
if (self.equals(search))
return this;
if (rightChild == null && leftChild == null)
return null;
if (rightChild != null && leftChild == null)
return rightChild.get(search);
if (leftChild != null && rightChild == null)
return leftChild.get(search);
TreeNode<T> rf = rightChild.get(search);
return (rf != null) ? rf : leftChild.get(search);
}
public TreeNode<T> getRightChild()
{
return rightChild;
}
public void setRightChild(TreeNode<T> rightChild)
{
this.rightChild = rightChild;
}
public TreeNode<T> getLeftChild()
{
return leftChild;
}
public void setLeftChild(TreeNode<T> leftChild)
{
this.leftChild = leftChild;
}
public int depth()
{
return depth(this);
}
public int depth(TreeNode<T> head)
{
if (head == null)
return 0;
else
return 1 + Math.max(depth(head.getLeftChild()), depth(head.getRightChild()));
}
public int size()
{
return size(this);
}
private int size(TreeNode<T> head)
{
if (head == null)
return 0;
else
return 1 + size(head.getLeftChild()) + size(head.getRightChild());
}
public void printPreOrder()
{
System.out.print(self + " ");
if (leftChild != null)
leftChild.printPreOrder();
if (rightChild != null)
rightChild.printPreOrder();
}
public void printPostOrder()
{
if (leftChild != null)
leftChild.printPostOrder();
if (rightChild != null)
rightChild.printPostOrder();
System.out.print(self + " ");
}
public void printInOrder()
{
if (leftChild != null)
leftChild.printInOrder();
System.out.print(self + " ");
if (rightChild != null)
rightChild.printInOrder();
}
public void prettyPrint()
{
BTreePrinter.printNode(this);
}
public String toString()
{
return self.toString();
}
public TreeNode<T> getParent()
{
return parent;
}
public void setParent(TreeNode<T> parent)
{
this.parent = parent;
}
}