-
Notifications
You must be signed in to change notification settings - Fork 7
/
TrinaryTree.java
177 lines (151 loc) · 3.76 KB
/
TrinaryTree.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
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
/**
*
* @author Luca Graziani
*
* This class represent a trinary tree
* It has methods for add and delete a node
*/
public class TrinaryTree {
public static class Node{
Node leftChild;
Node middleChild;
Node rightChild;
Node parent;
int value;
public Node(int value) {
this.parent = null;
this.leftChild = null;
this.rightChild = null;
this.middleChild = null;
this.value = value;
}
}
/**
* Create a new node and insert it in the tree
* @param treeNode root of the tree
* @param value value of the new node
*/
public void insert(Node treeNode, int value) {
// left insertion
if (value < treeNode.value) {
if(treeNode.leftChild == null){
treeNode.leftChild = new Node(value);
treeNode.leftChild.parent = treeNode;
}else{
insert(treeNode.leftChild,value);
}
return;
}
// right insertion
if (value > treeNode.value) {
if(treeNode.rightChild == null){
treeNode.rightChild = new Node(value);
treeNode.rightChild.parent = treeNode;
}else{
insert(treeNode.rightChild,value);
}
return;
}
// middle insertion
if(treeNode.middleChild == null){
treeNode.middleChild = new Node(value);
treeNode.middleChild.parent = treeNode;
}else{
insert(treeNode.middleChild,value);
}
}
/**
* Insert a node in a tree
* @param startingNode root of the tree
* @param newNode node to insert
*/
public void insert(Node startingNode, Node newNode) {
// left insertion
if (newNode.value < startingNode.value) {
if(startingNode.leftChild == null){
startingNode.leftChild = newNode;
startingNode.leftChild.parent = startingNode;
}else{
insert(startingNode.leftChild,newNode.value);
}
return;
}
// right insertion
if (newNode.value > startingNode.value) {
if(startingNode.rightChild == null){
startingNode.rightChild = newNode;
startingNode.rightChild.parent = startingNode;
}else{
insert(startingNode.rightChild,newNode.value);
}
return;
}
// middle insertion
if(startingNode.middleChild == null){
startingNode.middleChild = newNode;
startingNode.middleChild.parent = startingNode;
}else{
insert(startingNode.middleChild,newNode.value);
}
}
/**
* Find a node to delete
* In the node is not in the tree, returns null
* @param treeNode
* @param value
* @return
*/
public Node findNode(Node treeNode, int value) {
if (value < treeNode.value) {
return findNode(treeNode.leftChild,value);
}
if (value > treeNode.value) {
return findNode(treeNode.rightChild,value);
}
if (value == treeNode.value) {
// we found the node
return treeNode;
}
// node is not in the tree
return null;
}
/**
* Remove a node from the tree
* @param treeNode root of the tree
* @param value value of the node to remove
*/
public void delete(Node treeNode, int value) {
Node target = findNode(treeNode, value);
if(target == null){
// node is not in the tree
return;
}
// target has middleChild, so we delete the last of the middle child
if(target.middleChild != null){
while (target.middleChild != null){
target = target.middleChild;
}
target = null;
return;
}
// target has no children
if(target.rightChild == null && target.leftChild == null){
target = null;
return;
}
// at this point the terget nog could have left, right or both of those children
// target has left children
if(target.leftChild != null){
Node tmpNode = target.leftChild;
this.insert(tmpNode, tmpNode.parent.leftChild);
tmpNode.parent.leftChild = tmpNode;
}
// target has right children
if(target.rightChild != null){
Node tmpNode = target.rightChild;
this.insert(tmpNode.parent.rightChild, tmpNode);
}
target = null;
return;
}
}