-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
212 lines (194 loc) · 5.74 KB
/
BinaryTree.java
File metadata and controls
212 lines (194 loc) · 5.74 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.util.NoSuchElementException;
/**
* Class representing a Binary Search Tree with methods for inserting, deleting,
* in-order traversal, finding minimum value etc.
*
* @author Jimmy Lindstr�m
*
*/
public class BinaryTree {
protected TreeNode root;
protected int size;
/**
* Constructor for a binary tree, sets size to 0;
*/
public BinaryTree() {
size = 0;
}
/**
* Method for adding a value to the tree, calling the insert method.
*
* @param data the value to be added
*/
public void add(int data) {
try {
root = insert(root, new TreeNode(data));
size++;
} catch (Exception e) {
System.out.println(e);
}
}
/**
* Method for inserting a new Inlämmningsuppgift3.TreeNode in the tree.
*
* @param current the root of the tree
* @param newNode the new node to be inserted
* @return the new root of the tree
* @throws Exception if value already exists
*/
private TreeNode insert(TreeNode current, TreeNode newNode) throws Exception {
if (current == null) {
return newNode;
}
if (current.getData() == newNode.getData()) {
throw new Exception("The value already exists in the tree!");
} else if (newNode.getData() < current.getData()) {
current.setLeft(insert(current.getLeft(), newNode));
} else if (newNode.getData() > current.getData()) {
current.setRight(insert(current.getRight(), newNode));
}
return current;
}
/**
* Method for deleting a node, that calls the remove method. It sets the new
* root of the tree
*
* @param data the value to be deleted from the tree
*/
public void delete(int data) {
try {
root = remove(root, data);
size--;
} catch (NoSuchElementException e) {
System.out.println(e);
}
}
/**
* Method for removing a node from the tree
*
* @param current the root node
* @param dataToRemove the value you want to remove
* @return the new root of the tree
*/
private TreeNode remove(TreeNode current, int dataToRemove) {
TreeNode nodeToRemove = find(current, dataToRemove);
// If the value doesn't exist
if (nodeToRemove == null) {
throw new NoSuchElementException("Value does not exist in tree");
}
TreeNode parent = findParent(root, nodeToRemove.getData());
// If the node to be removed is a leaf(no children)
if (parent != null && nodeToRemove.isLeaf()) {
if (parent.getLeft() == nodeToRemove) {
parent.setLeft(null);
} else if (parent.getRight() == nodeToRemove) {
parent.setRight(null);
}
// if the node to be removed has one child
} else if (nodeToRemove.getLeft() == null || nodeToRemove.getRight() == null) {
if (parent != null) {
TreeNode tempNode = nodeToRemove.getLeft() == null
? nodeToRemove.getRight()
: nodeToRemove.getLeft();
if (parent.getLeft() == nodeToRemove) {
parent.setLeft(tempNode);
} else {
parent.setRight(tempNode);
}
}
// if the node to be removed has two children
} else {
TreeNode tempNode = findSuccessor(nodeToRemove);
int value = tempNode.getData();
delete(value);
nodeToRemove.setData(value);
}
return current;
}
/**
* Method for finding a node with a specific value in the tree
*
* @param current
* the node from where you want to start your search
* @param dataToFind
* the value you are searching for
* @return the node with the value you are searching for
*/
private TreeNode find(TreeNode current, int dataToFind) {
if(current == null){
return current;
}
if (current.getData() == dataToFind) {
return current;
}
if (current.getData() > dataToFind) {
return find(current.getLeft(), dataToFind);
} else {
return find(current.getRight(), dataToFind);
}
}
/**
* Method for finding a parent node of a node with a specified value
* @param current Node to start from.
* @param dataToFind The data of the child who's parent we're looking for.
* @return The parent node.
*/
public TreeNode findParent(TreeNode current, int dataToFind) {
if (current == null)
return null;
if ((current.getLeft() != null && dataToFind == current.getLeft().getData()) ||
(current.getRight() != null && dataToFind == current.getRight().getData()))
return current;
if (dataToFind < current.getData())
return findParent(current.getLeft(), dataToFind);
else
return findParent(current.getRight(), dataToFind);
}
/**
* Method for doing in-order traversal, meaning printing out all the values
* in ascending order
*
* @param current the node from where you want the traversal to begin
*/
public void inOrder(TreeNode current) {
if (current == null) {
return;
}
inOrder(current.getLeft());
System.out.println(current.getData() + " Have height: " + current.getHeight());
inOrder(current.getRight());
}
/**
* Method for finding the successor to a node. The successor next bigger
* number in the tree
*
* @param current node for which we want the successor
* @return the successor node
*/
public TreeNode findSuccessor(TreeNode current) {
if (current == null) {
return null;
}
// if node has a right child find minimum value of the right child
if (current.getRight() != null) {
return findMinimum(current.getRight());
}
return current;
}
/**
* Method for finding the smallest value in the tree rooted in the parameter
* current
*
* @param current the root of the tree/subtree
* @return node with smallest value in the tree/subtree
*/
private TreeNode findMinimum(TreeNode current) {
if (current == null) {
return null;
}
if (current.getLeft() != null) {
return findMinimum(current.getLeft());
}
return current;
}
}