Skip to content

Commit 8c38c60

Browse files
Merge pull request TheAlgorithms#14 from RianGallagher/master
Added binary tree
2 parents e0c07f4 + 2665382 commit 8c38c60

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

data_structures/BinaryTree.java

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
//A binary tree is a data structure in which an element has two successors(children)
2+
//The left child is usually smaller than the parent, and the right child is usually bigger
3+
class Node{
4+
public int data;
5+
public Node left;
6+
public Node right;
7+
public Node parent;
8+
9+
public Node(int value){
10+
data = value;
11+
left = null;
12+
right = null;
13+
parent = null;
14+
}
15+
}
16+
17+
class Tree{
18+
private Node root;
19+
20+
public Tree(){
21+
root = null;
22+
}
23+
//Returns the node if it finds it, otherwise returns the parent
24+
public Node find(int key){
25+
Node current = root;
26+
Node last = root;
27+
while(current != null){
28+
last = current;
29+
if(key < current.data)
30+
current = current.left;
31+
else if(key > current.data)
32+
current = current.right;
33+
//If you find the value return it
34+
else
35+
return current;
36+
}
37+
return last;
38+
}
39+
40+
//Inserts the given value
41+
public void put(int value){
42+
Node newNode = new Node(value);
43+
if(root == null)
44+
root = newNode;
45+
else{
46+
//This will return the soon to be parent of the value you're inserting
47+
Node parent = find(value);
48+
49+
//This if/else assigns the new node to be either the left or right child of the parent
50+
if(value < parent.data){
51+
parent.left = newNode;
52+
parent.left.parent = parent;
53+
return;
54+
}
55+
else{
56+
parent.right = newNode;
57+
parent.right.parent = parent;
58+
return;
59+
}
60+
}
61+
}
62+
63+
//Deletes the given value
64+
public boolean remove(int value){
65+
//temp is the node to be deleted
66+
Node temp = find(value);
67+
68+
//If the value doesn't exist
69+
if(temp.data != value)
70+
return false;
71+
72+
//No children
73+
if(temp.right == null && temp.left == null){
74+
if(temp == root)
75+
root = null;
76+
77+
//This if/else assigns the new node to be either the left or right child of the parent
78+
else if(temp.parent.data < temp.data)
79+
temp.parent.right = null;
80+
else
81+
temp.parent.left = null;
82+
return true;
83+
}
84+
85+
//Two children
86+
else if(temp.left != null && temp.right != null){
87+
Node succesor = findSuccesor(temp);
88+
89+
//The left tree of temp is made the left tree of the successor
90+
succesor.left = temp.left;
91+
succesor.left.parent = succesor;
92+
93+
//If the successor has a right child, the child's grandparent is it's new parent
94+
if(succesor.right != null && succesor.parent != temp){
95+
succesor.right.parent = succesor.parent;
96+
succesor.parent.left = succesor.right;
97+
succesor.right = temp.right;
98+
succesor.right.parent = succesor;
99+
}
100+
if(temp == root){
101+
succesor.parent = null;
102+
root = succesor;
103+
return true;
104+
}
105+
106+
//If you're not deleting the root
107+
else{
108+
succesor.parent = temp.parent;
109+
110+
//This if/else assigns the new node to be either the left or right child of the parent
111+
if(temp.parent.data < temp.data)
112+
temp.parent.right = succesor;
113+
else
114+
temp.parent.left = succesor;
115+
return true;
116+
}
117+
}
118+
//One child
119+
else{
120+
//If it has a right child
121+
if(temp.right != null){
122+
if(temp == root){
123+
root = temp.right; return true;}
124+
125+
temp.right.parent = temp.parent;
126+
127+
//Assigns temp to left or right child
128+
if(temp.data < temp.parent.data)
129+
temp.parent.left = temp.right;
130+
else
131+
temp.parent.right = temp.right;
132+
return true;
133+
}
134+
//If it has a left child
135+
else{
136+
if(temp == root){
137+
root = temp.left; return true;}
138+
139+
temp.left.parent = temp.parent;
140+
141+
//Assigns temp to left or right side
142+
if(temp.data < temp.parent.data)
143+
temp.parent.left = temp.left;
144+
else
145+
temp.parent.right = temp.left;
146+
return true;
147+
}
148+
}
149+
}
150+
151+
//Move right once and go left down the tree as far as you can
152+
public Node findSuccesor(Node n){
153+
if(n.right == null)
154+
return n;
155+
Node current = n.right;
156+
Node parent = n.right;
157+
while(current != null){
158+
parent = current;
159+
current = current.left;
160+
}
161+
return parent;
162+
}
163+
164+
public Node getRoot(){
165+
return root;
166+
}
167+
168+
//Prints leftChild - root - rightChild
169+
public void inOrder(Node localRoot){
170+
if(localRoot != null){
171+
inOrder(localRoot.left);
172+
System.out.print(localRoot.data + " ");
173+
inOrder(localRoot.right);
174+
}
175+
}
176+
//Prints root - leftChild - rightChild
177+
public void preOrder(Node localRoot){
178+
if(localRoot != null){
179+
System.out.print(localRoot.data + " ");
180+
preOrder(localRoot.left);
181+
preOrder(localRoot.right);
182+
}
183+
}
184+
//Prints rightChild - leftChild - root
185+
public void postOrder(Node localRoot){
186+
if(localRoot != null){
187+
postOrder(localRoot.left);
188+
postOrder(localRoot.right);
189+
System.out.print(localRoot.data + " ");
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy