Skip to content

Commit 74fbcac

Browse files
committed
#Modification 29
1 parent b490ddc commit 74fbcac

4 files changed

Lines changed: 137 additions & 2 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Deletion_of_a_node_in_bst
3+
*/
4+
public class Deletion_of_a_node_in_bst {
5+
class Node {
6+
int key;
7+
Node left, right;
8+
9+
Node(int item) {
10+
key = item;
11+
left = right = null;
12+
}
13+
}
14+
15+
Node root;
16+
17+
Deletion_of_a_node_in_bst() {
18+
root = null;
19+
}
20+
21+
void deleteKey(int key) {
22+
root = deleteRec(root, key);
23+
}
24+
25+
Node deleteRec(Node root, int key) {
26+
if (root == null)
27+
return root;
28+
if (key < root.key)
29+
root.left = deleteRec(root.left, key);
30+
else if (key > root.key)
31+
root.right = deleteRec(root.right, key);
32+
else {
33+
if (root.left == null)
34+
return root.right;
35+
else if (root.right == null)
36+
return root.left;
37+
38+
root.key = minValue(root.right);
39+
root.right = deleteRec(root.right, root.key);
40+
}
41+
return root;
42+
}
43+
44+
int minValue(Node root) {
45+
int minv = root.key;
46+
while (root.left != null) {
47+
minv = root.left.key;
48+
root = root.left;
49+
}
50+
return minv;
51+
}
52+
53+
void insert(int key) {
54+
root = insertRec(root, key);
55+
}
56+
57+
Node insertRec(Node root, int key) {
58+
if (root == null) {
59+
root = new Node(key);
60+
return root;
61+
}
62+
if (key < root.key)
63+
root.left = insertRec(root.left, key);
64+
else if (key > root.key)
65+
root.right = insertRec(root.right, key);
66+
return root;
67+
}
68+
69+
void inorder() {
70+
inorderRec(root);
71+
}
72+
73+
void inorderRec(Node root) {
74+
if (root != null)
75+
inorderRec(root.left);
76+
System.out.print(root.key + " ");
77+
inorderRec(root.right);
78+
79+
}
80+
81+
public static void main(String[] args) {
82+
Deletion_of_a_node_in_bst tree = new Deletion_of_a_node_in_bst();
83+
// * Let us create following BST
84+
// ? 50
85+
// ? / \
86+
// ? 30 70
87+
// ? / \ / \
88+
// ?20 40 60 80 */
89+
tree.insert(50);
90+
tree.insert(30);
91+
tree.insert(20);
92+
tree.insert(40);
93+
tree.insert(70);
94+
tree.insert(60);
95+
tree.insert(80);
96+
97+
System.out.println("Inorder traversal of the given tree");
98+
tree.inorder();
99+
100+
System.out.println("\nDelete 20");
101+
tree.deleteKey(20);
102+
System.out.println("Inorder traversal of the modified tree");
103+
tree.inorder();
104+
105+
System.out.println("\nDelete 30");
106+
tree.deleteKey(30);
107+
System.out.println("Inorder traversal of the modified tree");
108+
tree.inorder();
109+
110+
System.out.println("\nDelete 50");
111+
tree.deleteKey(50);
112+
System.out.println("Inorder traversal of the modified tree");
113+
tree.inorder();
114+
}
115+
}

binary_search_tree/Find_a_value_in_bst.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package binary_search_tree;
2-
31
public class Find_a_value_in_bst {
42

53
class Node {
581 Bytes
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package bit_manipulation;
2+
3+
import java.io.*;
4+
5+
public class Count_set_bit_in_integer {
6+
/*
7+
* Function to get no of set bits in binary representation of positive integer n
8+
*/
9+
static int countSetBits(int n) {
10+
int count = 0;
11+
while (n > 0) {
12+
count += n & 1;
13+
n >>= 1;
14+
}
15+
return count;
16+
}
17+
18+
public static void main(String[] args) {
19+
int i = 9;
20+
System.out.println(countSetBits(i));
21+
}
22+
}

0 commit comments

Comments
 (0)