FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code/Algorithm/BinarySearchTree.java at master · dwipam/code · GitHub
dwipam
/
code
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
7
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
code
/
Algorithm
/
BinarySearchTree.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
169 lines (160 loc) · 3.63 KB
Breadcrumbs
code
/
Algorithm
/
BinarySearchTree.java
Copy path
File metadata and controls
executable file
·
169 lines (160 loc) · 3.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.
Random
;
import
java
.
util
.
Scanner
;
public
class
BinarySearchTree
{
public
static
Node
root
;
public
BinarySearchTree
(){
this
.
root
=
null
;
}
public
boolean
find
(
int
id
){
Node
current
=
root
;
while
(
current
!=
null
){
if
(
current
.
data
==
id
){
return
true
;
}
else
if
(
current
.
data
>
id
){
current
=
current
.
left
;
}
else
{
current
=
current
.
right
;
}
}
return
false
;
}
public
boolean
delete
(
int
id
){
Node
parent
=
root
;
Node
current
=
root
;
boolean
isLeftChild
=
false
;
while
(
current
.
data
!=
id
){
parent
=
current
;
if
(
current
.
data
>
id
){
isLeftChild
=
true
;
current
=
current
.
left
;
}
else
{
isLeftChild
=
false
;
current
=
current
.
right
;
}
if
(
current
==
null
){
return
false
;
}
}
//if i am here that means we have found the node
//Case 1: if node to be deleted has no children
if
(
current
.
left
==
null
&&
current
.
right
==
null
){
if
(
current
==
root
){
root
=
null
;
}
if
(
isLeftChild
==
true
){
parent
.
left
=
null
;
}
else
{
parent
.
right
=
null
;
}
}
else
if
(
current
.
right
==
null
){
if
(
current
==
root
){
root
=
current
.
left
;
}
else
if
(
isLeftChild
){
parent
.
left
=
current
.
left
;
}
else
{
parent
.
right
=
current
.
left
;
}
}
else
if
(
current
.
left
==
null
){
if
(
current
==
root
){
root
=
current
.
right
;
}
else
if
(
isLeftChild
){
parent
.
left
=
current
.
right
;
}
else
{
parent
.
right
=
current
.
right
;
}
}
else
if
(
current
.
left
!=
null
&&
current
.
right
!=
null
){
Node
successor
=
getSuccessor
(
current
);
if
(
current
==
root
){
root
=
successor
;
}
else
if
(
isLeftChild
){
parent
.
left
=
successor
;
}
else
{
parent
.
right
=
successor
;
}
successor
.
left
=
current
.
left
;
}
return
true
;
}
public
Node
getSuccessor
(
Node
deleleNode
){
Node
successsor
=
null
;
Node
successsorParent
=
null
;
Node
current
=
deleleNode
.
right
;
while
(
current
!=
null
){
successsorParent
=
successsor
;
successsor
=
current
;
current
=
current
.
left
;
}
if
(
successsor
!=
deleleNode
.
right
){
successsorParent
.
left
=
successsor
.
right
;
successsor
.
right
=
deleleNode
.
right
;
}
return
successsor
;
}
public
void
insert
(
int
id
){
Node
newNode
=
new
Node
(
id
);
if
(
root
==
null
){
root
=
newNode
;
return
;
}
Node
current
=
root
;
Node
parent
=
null
;
while
(
true
){
parent
=
current
;
if
(
id
<
current
.
data
){
current
=
current
.
left
;
if
(
current
==
null
){
parent
.
left
=
newNode
;
return
;
}
}
else
{
current
=
current
.
right
;
if
(
current
==
null
){
parent
.
right
=
newNode
;
return
;
}
}
}
}
public
void
disp
(
Node
root
){
if
(
root
!=
null
){
disp
(
root
.
left
);
System
.
out
.
print
(
" "
+
root
.
data
);
disp
(
root
.
right
);
}
}
public
static
void
main
(
String
arg
[]){
BinarySearchTree
bst
=
new
BinarySearchTree
();
Scanner
in
=
new
Scanner
(
System
.
in
);
int
[]
array
=
new
int
[
10
];
Random
randomGenerator
=
new
Random
();
for
(
int
i
=
1
;
i
<
10
;
i
++){
array
[
i
]=
randomGenerator
.
nextInt
(
100
);
System
.
out
.
print
(
array
[
i
] +
" "
);
}
for
(
int
i
=
1
;
i
<
10
;
i
++)
bst
.
insert
(
array
[
i
]);
System
.
out
.
println
(
""
);
System
.
out
.
println
(
"Enter the value to search : "
);
int
fin
=
in
.
nextInt
();
if
(
bst
.
find
(
fin
) ==
false
)
System
.
out
.
println
(
"Not Found"
);
else
System
.
out
.
println
(
"Found"
);
/*System.out.println("Enter the Node to delete : ");
int del = in.nextInt();
bst.delete(del);*/
bst
.
disp
(
root
);
}
}
class
Node
{
int
data
;
Node
left
;
Node
right
;
public
Node
(
int
data
){
this
.
data
=
data
;
left
=
null
;
right
=
null
;
}
}
Back
|
FazBrowse Home
|
New Git URL