-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.java
More file actions
171 lines (161 loc) · 2.82 KB
/
binarySearchTree.java
File metadata and controls
171 lines (161 loc) · 2.82 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
169
170
171
public class binarySearchTree {
Node root;
binarySearchTree(){
this.root = null;
}
int findMin(Node r) {
if(r == null) {
return -1;
}
else if(r.left == null) {
return r.data;
}
else {
return findMin(r.left);
}
}
int findMax(Node r) {
if(r == null) {
return -1;
}
else if(r.right == null) {
return r.data;
}
else {
return findMax(r.right);
}
}
Node insert(Node r,int d)
{
if(r==null)
{
Node n=new Node(d);
r=n;
return r;
}
if(r.data<d) {
r.right=insert(r.right,d);
}
else if(r.data>d)
{
r.left=insert(r.left,d);
}
return r;
}
Node del(Node r, int d) {
if(r==null) {
return r;
}
if(r.data>d) {
r.left = del(r.left,d);
}
else if(r.data<d) {
r.right = del(r.right,d);
}
else {
if(r.right == null && r.left == null) {
return null;
}
else if(r.right == null) {
r.left = del(r.left,d);
}
else if(r.left == null) {
r.right = del(r.right,d);
}
else {
Node insucc = new Node(findMin(r.right));
r.data = insucc.data;
r.right = del(r.right,d);
}
}
return r;
}
boolean search(Node r , int element) {
if(r == null) {
return false;
}
if(r.data == element) {
return true;
}
else {
if(r.data>element) {
return search(r.left,element);
}
else {
return search(r.right,element);
}
}
}
void inorder(Node r) {
if(r == null) {
return;
}
inorder(r.left);
System.out.println(r.data);
inorder(r.right);
}
void preOrder(Node r) {
if(r == null) {
return;
}
System.out.println(r.data);
preOrder(r.left);
preOrder(r.right);
}
void postOrder(Node r) {
if(r == null) {
return;
}
System.out.println(r.data);
postOrder(r.left);
postOrder(r.right);
}
int height(Node r) {
if(r == null) {
return 0;
}
int right = 1+height(r.right);
int left = 1+height(r.left);
if(left>right) {
return left;
}
else {
return right;
}
}
class count {
int c = 0;
}
void kthLargestUtil(Node node,int k, count C) {
if (node == null || C.c >= k)
return;
this.kthLargestUtil(node.right, k, C);
C.c++;
if (C.c == k) {
System.out.println(k + "th largest element is " +
node.data);
return;
}
this.kthLargestUtil(node.left, k, C);
}
void kthLargest(int k)
{
count C = new count();
this.kthLargestUtil(this.root, k, C);
}
/*void kthSmallestUtil(Node r,int k, count q) {
if(k == q.c) {
return;
}
this.kthSmallestUtil(r.left,k,q);
q.c++;
if(q.c==k) {
System.out.println(r.data);
}
this.kthSmallestUtil(r.right,k,q);
}
void kthSmallest(int k) {
count q = new count();
this.kthSmallestUtil(this.root,k,q);
}*/
}