forked from hackprot0/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfast_tree_search
More file actions
168 lines (148 loc) · 5.18 KB
/
fast_tree_search
File metadata and controls
168 lines (148 loc) · 5.18 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
// binary search tree creation, new node insertion , and node deletion
import java.util.*;
class bstree{
//node structure
nodes headp;
static class nodes{
nodes left;
int value;
nodes right;
}
//bst creation
void bstreec(){
int size;
nodes duph=headp;
Scanner in= new Scanner(System.in);
System.out.println("enter the no of nodes you want to insert");
int no=in.nextInt(); //no of nodes asked
for(int i=0;i<no;i++)
{
if(headp == null){ //if our root is null first create root node
nodes obj = new nodes ();
System.out.println("enter the root node");
int value=in.nextInt();
obj.value=value;
headp=obj;
duph=headp;
} // if root is assigned or had been created then create futher
else{
duph=headp;
System.out.println("enter next node");
int value=in.nextInt();
nodes obj = new nodes();
obj.value=value;
while(duph != null){ //traversing the tree from root node till we find the position to insert the given node and inserting it
if(value < duph.value && duph.left != null){
duph=duph.left;
}
else if(value >duph.value && duph.right != null){
duph=duph.right;
}
else if( value <duph.value){
duph.left=obj;
duph=duph.left; // now going on my newly node which is inserted
duph=duph.left; // going to left of my newly node(it dosent matter you can go right also)
}
else{
duph.right=obj;
duph=duph.right; // now going on my newly node which is inserted
duph=duph.right; // going to right of my newly node(it dosent matter you can go left also)
}
}
}
}
}
//insertion of node
void insert(int value){ //taking the value
nodes duph=headp;
nodes obj = new nodes();
obj.value=value;
while(duph != null){ //travrsing the whol
if(value < duph.value && duph.left != null){
duph=duph.left;
}
else if(value >duph.value && duph.right != null){
duph=duph.right;
}
else if( value <duph.value){
duph.left=obj;
duph=duph.left; //same explanation as explained above
duph=duph.left; //same explanation as explained above
}
else{
duph.right=obj;
duph=duph.right;
duph=duph.right;
}
}
}
// Get minimum element in binary search tree
public nodes minimumElement(nodes root) {
if (root.left == null)
return root;
else {
return minimumElement(root.left);
}
}
public nodes deleteNode(nodes root, int value) {
if (root == null)
return null;
if (root.value > value) {
root.left = deleteNode(root.left, value);
} else if (root.value < value) {
root.right = deleteNode(root.right, value);
} else {
// if nodeToBeDeleted have both children
if (root.left != null && root.right != null) {
nodes temp = root;
// Finding minimum element from right
nodes minNodeForRight = minimumElement(temp.right);
// Replacing current node with minimum node from right subtree
root.value = minNodeForRight.value;
// Deleting minimum node from right now
root.right = deleteNode(root.right, minNodeForRight.value);
}
// if nodeToBeDeleted has only left child
else if (root.left != null) {
root = root.left;
}
// if nodeToBeDeleted has only right child
else if (root.right != null) {
root = root.right;
}
// if nodeToBeDeleted do not have child (Leaf node)
else
root = null;
}
return root;
}
public void treet(nodes poi){
//recursive
//tree traversal
if(poi != null){
System.out.println(poi.value);
treet(poi.left);
treet(poi.right);
}
}
}
public class binary_search_tree{
public static void main(String [] arrrr){
Scanner wow = new Scanner(System.in);
//creting object
bstree obj = new bstree();
obj.bstreec();
System.out.println("tree :-");
obj.treet(obj.headp); //display of tree
//calling to insert
System.out.println("enter the value you wanna insert");
int ni=wow.nextInt();
obj.insert(ni);
//calling for deletion of node
System.out.println("enter the value you wanna delete");
int val=wow.nextInt();
obj.deleteNode(obj.headp,val);
System.out.println("tree afterd:-");
obj.treet(obj.headp); //display of tree after deleting the node
}
}