Binary Tree code by linked list

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
package com.chung;
import java.util.*;

import node.BinaryNode;

public class BinaryTreeByLinkedList {
BinaryNode root;



//Constructor for creating a blank Binary Tree
BinaryTreeByLinkedList(){
this.root = null;
}


// inserts a new node at deepest place in Tree
void insert(int value) {
BinaryNode node = new BinaryNode();
node.setValue(value);
if (root == null) {
root = node;
System.out.println("Successfully inserted new node at Root !");
return;
}
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
while (!queue.isEmpty()) {
BinaryNode presentNode = queue.remove();
if (presentNode.getLeft() == null) {
presentNode.setLeft(node);
System.out.println("Successfully inserted new node !");
break;
}else if (presentNode.getRight() == null) {
presentNode.setRight(node);
System.out.println("Successfully inserted new node !");
break;
} else {
queue.add(presentNode.getLeft());
queue.add(presentNode.getRight());
}//end of else-if
}//end of loop
}//end of method


// Search for a given value in binary tree
void search(int value) {
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
while (!queue.isEmpty()) {
BinaryNode presentNode = queue.remove();
if (presentNode.getValue() == value) {
System.out.println("Value-"+value+" is found in Tree !");
return;
}else {
if (presentNode.getLeft()!=null)
queue.add(presentNode.getLeft());
if (presentNode.getRight()!=null)
queue.add(presentNode.getRight());
}
}//end of loop
System.out.println("Value-"+value+" is not found in Tree !");
}//end of method


// delete node from binary tree
void deleteNodeOfBinaryTree(int value) {
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
while (!queue.isEmpty()) {
BinaryNode presentNode = queue.remove();
// if node is found then copy deepest node here and delete deepest node.
if (presentNode.getValue() == value) {
presentNode.setValue(getDeepestNode().getValue());
DeleteDeepestNode();
System.out.println("Deleted the node !!");
return;
}else {
if (presentNode.getLeft() != null)
queue.add(presentNode.getLeft());
if (presentNode.getRight() != null)
queue.add(presentNode.getRight());
}
}//end of while loop
System.out.println("Did not find the node!!");
}


//Delete deepest node
public void DeleteDeepestNode() {
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
BinaryNode previousNode, presentNode = null;
while (!queue.isEmpty()) {
previousNode = presentNode;
presentNode = queue.remove();
if (presentNode.getLeft() == null) {
previousNode.setRight(null);
return;
}else if ((presentNode.getRight() == null)) {
presentNode.setLeft(null);
return;
}
queue.add(presentNode.getLeft());
queue.add(presentNode.getRight());
}//end of while loop
}//end of method



// get last node of last level of binary tree
public BinaryNode getDeepestNode() {
// make an empty queue. Queue is Interface and LinkedList is class
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
BinaryNode presentNode = null;
while (!queue.isEmpty()) {
presentNode = queue.remove();
if (presentNode.getLeft() != null)
queue.add(presentNode.getLeft());
if (presentNode.getRight() != null)
queue.add(presentNode.getRight());
}
return presentNode;
}//end of method



// pre-order traversal of binary tree
void preOrder(BinaryNode node) {
if (node == null)
return;
System.out.print(node.getValue() + " ");
preOrder(node.getLeft());
preOrder(node.getRight());
}//end of method



// post-order traversal of binary tree
void postOrder(BinaryNode node) {
if (node == null)
return;
postOrder(node.getLeft());
postOrder(node.getRight());
System.out.print(node.getValue() + " ");
}//end of method



// in-order traversal of binary tree
void inOrder(BinaryNode node) {
if (node == null) {
return;
}
inOrder(node.getLeft());
System.out.print(node.getValue() + " ");
inOrder(node.getRight());
}//end of method



// level order traversal of binary tree
void levelOrder() {
// make a queue for level order. Queue is Interface and LinkedList is class
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
while (!queue.isEmpty()) {
BinaryNode presentNode = queue.remove();
System.out.print(presentNode.getValue() + " ");
if (presentNode.getLeft() != null) {
queue.add(presentNode.getLeft());
}
if (presentNode.getRight() != null)
queue.add(presentNode.getRight());
}
}// end of method


// Delete Tree
void deleteTree() {
root = null;
System.out.println("Binary Tree has been deleted successfully");
}

}//end of class