Skip to content

Commit 9b18ee3

Browse files
committed
Updated BinaryTree and BinaryTreeTest with encapsulation and test cases
1 parent 3c9aac6 commit 9b18ee3

File tree

2 files changed

+47
-225
lines changed

2 files changed

+47
-225
lines changed

src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java

Lines changed: 36 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -3,117 +3,60 @@
33
import java.util.LinkedList;
44
import java.util.Queue;
55

6-
/*
7-
* This entire class is used to build a Binary Tree data structure. There is the
8-
* Node Class and the Tree Class, both explained below.
9-
*/
106
/**
11-
* A binary tree is a data structure in which an element has two
12-
* successors(children). The left child is usually smaller than the parent, and
13-
* the right child is usually bigger.
7+
* A binary tree data structure where elements have two successors (children).
8+
* The left child is smaller than the parent, and the right child is larger.
149
*
1510
* @author Unknown
1611
*/
1712
public class BinaryTree {
1813

1914
/**
20-
* This class implements the nodes that will go on the Binary Tree. They
21-
* consist of the data in them, the node to the left, the node to the right,
22-
* and the parent from which they came from.
23-
*
24-
* @author Unknown
15+
* Node class represents elements in the Binary Tree, with pointers to
16+
* the left and right children and a reference to its parent node.
2517
*/
2618
static class Node {
27-
28-
/**
29-
* Data for the node
30-
*/
3119
public int data;
32-
/**
33-
* The Node to the left of this one
34-
*/
35-
public Node left;
36-
/**
37-
* The Node to the right of this one
38-
*/
39-
public Node right;
40-
/**
41-
* The parent of this node
42-
*/
43-
public Node parent;
20+
public Node left, right, parent;
4421

45-
/**
46-
* Constructor of Node
47-
*
48-
* @param value Value to put in the node
49-
*/
50-
Node(int value) {
22+
public Node(int value) {
5123
data = value;
52-
left = null;
53-
right = null;
54-
parent = null;
24+
left = right = parent = null;
5525
}
5626
}
5727

58-
/**
59-
* The root of the Binary Tree
60-
*/
6128
private Node root;
6229

63-
/**
64-
* Constructor
65-
*/
6630
public BinaryTree() {
6731
root = null;
6832
}
6933

70-
/**
71-
* Parameterized Constructor
72-
*/
7334
public BinaryTree(Node root) {
7435
this.root = root;
7536
}
7637

77-
/**
78-
* Method to find a Node with a certain value
79-
*
80-
* @param key Value being looked for
81-
* @return The node if it finds it, otherwise returns the parent
82-
*/
8338
public Node find(int key) {
8439
Node current = root;
8540
while (current != null) {
8641
if (key < current.data) {
87-
if (current.left == null) {
88-
return current; // The key isn't exist, returns the parent
89-
}
42+
if (current.left == null) return current;
9043
current = current.left;
9144
} else if (key > current.data) {
92-
if (current.right == null) {
93-
return current;
94-
}
45+
if (current.right == null) return current;
9546
current = current.right;
96-
} else { // If you find the value return it
47+
} else {
9748
return current;
9849
}
9950
}
10051
return null;
10152
}
10253

103-
/**
104-
* Inserts certain value into the Binary Tree
105-
*
106-
* @param value Value to be inserted
107-
*/
10854
public void put(int value) {
10955
Node newNode = new Node(value);
11056
if (root == null) {
11157
root = newNode;
11258
} else {
113-
// This will return the soon to be parent of the value you're inserting
11459
Node parent = find(value);
115-
116-
// This if/else assigns the new node to be either the left or right child of the parent
11760
if (value < parent.data) {
11861
parent.left = newNode;
11962
parent.left.parent = parent;
@@ -124,141 +67,59 @@ public void put(int value) {
12467
}
12568
}
12669

127-
/**
128-
* Deletes a given value from the Binary Tree
129-
*
130-
* @param value Value to be deleted
131-
* @return If the value was deleted
132-
*/
13370
public boolean remove(int value) {
134-
// temp is the node to be deleted
13571
Node temp = find(value);
72+
if (temp == null || temp.data != value) return false;
13673

137-
// If the value doesn't exist
138-
if (temp.data != value) {
139-
return false;
140-
}
141-
142-
// No children
143-
if (temp.right == null && temp.left == null) {
144-
if (temp == root) {
145-
root = null;
146-
} // This if/else assigns the new node to be either the left or right child of the
147-
// parent
148-
else if (temp.parent.data < temp.data) {
149-
temp.parent.right = null;
150-
} else {
151-
temp.parent.left = null;
152-
}
74+
if (temp.left == null && temp.right == null) {
75+
if (temp == root) root = null;
76+
else if (temp.parent.data < temp.data) temp.parent.right = null;
77+
else temp.parent.left = null;
15378
return true;
154-
} // Two children
155-
else if (temp.left != null && temp.right != null) {
79+
} else if (temp.left != null && temp.right != null) {
15680
Node successor = findSuccessor(temp);
157-
158-
// The left tree of temp is made the left tree of the successor
15981
successor.left = temp.left;
16082
successor.left.parent = successor;
161-
162-
// If the successor has a right child, the child's grandparent is it's new parent
16383
if (successor.parent != temp) {
16484
if (successor.right != null) {
16585
successor.right.parent = successor.parent;
16686
successor.parent.left = successor.right;
167-
} else {
168-
successor.parent.left = null;
169-
}
87+
} else successor.parent.left = null;
17088
successor.right = temp.right;
17189
successor.right.parent = successor;
17290
}
173-
17491
if (temp == root) {
175-
successor.parent = null;
17692
root = successor;
177-
} // If you're not deleting the root
178-
else {
93+
successor.parent = null;
94+
} else {
17995
successor.parent = temp.parent;
180-
181-
// This if/else assigns the new node to be either the left or right child of the
182-
// parent
183-
if (temp.parent.data < temp.data) {
184-
temp.parent.right = successor;
185-
} else {
186-
temp.parent.left = successor;
187-
}
96+
if (temp.parent.data < temp.data) temp.parent.right = successor;
97+
else temp.parent.left = successor;
18898
}
18999
return true;
190-
} // One child
191-
else {
192-
// If it has a right child
193-
if (temp.right != null) {
194-
if (temp == root) {
195-
root = temp.right;
196-
return true;
197-
}
198-
199-
temp.right.parent = temp.parent;
200-
201-
// Assigns temp to left or right child
202-
if (temp.data < temp.parent.data) {
203-
temp.parent.left = temp.right;
204-
} else {
205-
temp.parent.right = temp.right;
206-
}
207-
} // If it has a left child
100+
} else {
101+
Node child = (temp.right != null) ? temp.right : temp.left;
102+
if (temp == root) root = child;
208103
else {
209-
if (temp == root) {
210-
root = temp.left;
211-
return true;
212-
}
213-
214-
temp.left.parent = temp.parent;
215-
216-
// Assigns temp to left or right side
217-
if (temp.data < temp.parent.data) {
218-
temp.parent.left = temp.left;
219-
} else {
220-
temp.parent.right = temp.left;
221-
}
104+
child.parent = temp.parent;
105+
if (temp.data < temp.parent.data) temp.parent.left = child;
106+
else temp.parent.right = child;
222107
}
223108
return true;
224109
}
225110
}
226111

227-
/**
228-
* This method finds the Successor to the Node given. Move right once and go
229-
* left down the tree as far as you can
230-
*
231-
* @param n Node that you want to find the Successor of
232-
* @return The Successor of the node
233-
*/
234112
public Node findSuccessor(Node n) {
235-
if (n.right == null) {
236-
return n;
237-
}
113+
if (n.right == null) return n;
238114
Node current = n.right;
239-
Node parent = n.right;
240-
while (current != null) {
241-
parent = current;
242-
current = current.left;
243-
}
244-
return parent;
115+
while (current.left != null) current = current.left;
116+
return current;
245117
}
246118

247-
/**
248-
* Returns the root of the Binary Tree
249-
*
250-
* @return the root of the Binary Tree
251-
*/
252119
public Node getRoot() {
253120
return root;
254121
}
255122

256-
/**
257-
* Prints leftChild - root - rightChild This is the equivalent of a depth
258-
* first search
259-
*
260-
* @param localRoot The local root of the binary tree
261-
*/
262123
public void inOrder(Node localRoot) {
263124
if (localRoot != null) {
264125
inOrder(localRoot.left);
@@ -267,11 +128,6 @@ public void inOrder(Node localRoot) {
267128
}
268129
}
269130

270-
/**
271-
* Prints root - leftChild - rightChild
272-
*
273-
* @param localRoot The local root of the binary tree
274-
*/
275131
public void preOrder(Node localRoot) {
276132
if (localRoot != null) {
277133
System.out.print(localRoot.data + " ");
@@ -280,11 +136,6 @@ public void preOrder(Node localRoot) {
280136
}
281137
}
282138

283-
/**
284-
* Prints leftChild - rightChild - root
285-
*
286-
* @param localRoot The local root of the binary tree
287-
*/
288139
public void postOrder(Node localRoot) {
289140
if (localRoot != null) {
290141
postOrder(localRoot.left);
@@ -293,38 +144,15 @@ public void postOrder(Node localRoot) {
293144
}
294145
}
295146

296-
/**
297-
* Prints the tree in a breadth first search order This is similar to
298-
* pre-order traversal, but instead of being implemented with a stack (or
299-
* recursion), it is implemented with a queue
300-
*
301-
* @param localRoot The local root of the binary tree
302-
*/
303147
public void bfs(Node localRoot) {
304-
// Create a queue for the order of the nodes
148+
if (localRoot == null) return;
305149
Queue<Node> queue = new LinkedList<>();
306-
307-
// If the give root is null, then we don't add to the queue
308-
// and won't do anything
309-
if (localRoot != null) {
310-
queue.add(localRoot);
311-
}
312-
313-
// Continue until the queue is empty
150+
queue.add(localRoot);
314151
while (!queue.isEmpty()) {
315-
// Get the next node on the queue to visit
316-
localRoot = queue.remove();
317-
318-
// Print the data from the node we are visiting
319-
System.out.print(localRoot.data + " ");
320-
321-
// Add the children to the queue if not null
322-
if (localRoot.right != null) {
323-
queue.add(localRoot.right);
324-
}
325-
if (localRoot.left != null) {
326-
queue.add(localRoot.left);
327-
}
152+
Node node = queue.remove();
153+
System.out.print(node.data + " ");
154+
if (node.left != null) queue.add(node.left);
155+
if (node.right != null) queue.add(node.right);
328156
}
329157
}
330158
}

0 commit comments

Comments
 (0)