Skip to content

Commit d8cd225

Browse files
committed
Self Balancing Tree
1 parent ba79179 commit d8cd225

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* Class node is defined as :
2+
class Node
3+
int val; //Value
4+
int ht; //Height
5+
Node left; //Left child
6+
Node right; //Right child
7+
8+
*/
9+
10+
static Node insert(Node root,int val) {
11+
Node newNode = new Node();
12+
newNode.val = val;
13+
newNode.ht = 0;
14+
newNode.left = null;
15+
newNode.right = null;
16+
17+
if (root==null) {
18+
root = newNode;
19+
} else {
20+
root=avlinsert(newNode, root);
21+
}
22+
23+
return root;
24+
}
25+
26+
// return height of tree rooted at x
27+
static public int height(Node x) {
28+
if (x == null) return -1;
29+
else return x.ht;
30+
}
31+
32+
static public Node rotatewithleft(Node c) {
33+
Node p; // left child of c
34+
35+
p = c.left;
36+
c.left = p.right;
37+
p.right = c;
38+
39+
c.ht = Math.max(height(c.left), height(c.right)) + 1;
40+
p.ht = Math.max(height(p.left), height(p.right)) + 1;
41+
42+
return p;
43+
}
44+
45+
static public Node rotatewithright(Node c) {
46+
Node p; // right child of c
47+
48+
p = c.right;
49+
c.right = p.left;
50+
p.left = c;
51+
52+
c.ht = Math.max(height(c.left), height(c.right)) + 1;
53+
p.ht = Math.max(height(p.left), height(p.right)) + 1;
54+
55+
return p;
56+
}
57+
58+
static public Node doublerotatewithleft(Node c) {
59+
Node tmp;
60+
61+
c.left = rotatewithright(c.left);
62+
tmp = rotatewithleft(c);
63+
64+
return tmp;
65+
}
66+
67+
static public Node doublerotatewithright(Node c) {
68+
Node tmp;
69+
70+
c.right = rotatewithleft(c.right);
71+
tmp = rotatewithright(c);
72+
73+
return tmp;
74+
}
75+
76+
static public Node avlinsert(Node newNode, Node par) {
77+
Node newpar = par; // root of subtree par
78+
79+
if (newNode.val < par.val) {
80+
if (par.left == null) {
81+
par.left = newNode; //attach new node as leaf
82+
}
83+
else {
84+
par.left = avlinsert(newNode, par.left); // branch left
85+
if ((height(par.left) - height(par.right)) == 2) {
86+
if (newNode.val < par.left.val) {
87+
newpar=rotatewithleft(par);
88+
} else {
89+
newpar=doublerotatewithleft(par);
90+
} //else
91+
} //if
92+
} // else
93+
} // if
94+
else if (newNode.val > par.val) { // branch right
95+
if (par.right == null) {
96+
par.right = newNode; // attach new node as leaf
97+
} else {
98+
par.right = avlinsert(newNode, par.right); // branch right
99+
if ((height(par.right) - height(par.left)) == 2) {
100+
if (newNode.val > par.right.val) {
101+
newpar=rotatewithright(par);
102+
} //if
103+
else {
104+
newpar=doublerotatewithright(par);
105+
} //else
106+
} //if
107+
} //else
108+
} // else if
109+
110+
// Update the height, just in case
111+
112+
if ((par.left == null) && (par.right != null))
113+
par.ht = par.right.ht + 1;
114+
else if ((par.right == null) && (par.left != null))
115+
par.ht = par.left.ht + 1;
116+
else
117+
par.ht = Math.max(height(par.left), height(par.right)) + 1;
118+
119+
return newpar; // return new root of this subtree
120+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div align="center">
22
<img src="https://github.com/alexprut/HackerRank/raw/master/hackerrank-logo.png" width="450" height="auto"/>
33

4-
[![Solutions](https://img.shields.io/badge/solutions-408-green.svg?style=flat-square)](https://github.com/alexprut/HackerRank#table-of-contents)
4+
[![Solutions](https://img.shields.io/badge/solutions-409-green.svg?style=flat-square)](https://github.com/alexprut/HackerRank#table-of-contents)
55
[![Languages](https://img.shields.io/badge/languages-c%2B%2B%2Cjava%2Chaskell-yellow.svg)](https://github.com/alexprut/HackerRank#table-of-contents)
66
[![Author](https://img.shields.io/badge/author-alexprut-brightgreen.svg?style=flat-square)](https://www.hackerrank.com/alexprut)
77
[![MIT](https://img.shields.io/dub/l/vibe-d.svg?style=flat-square)](https://github.com/alexprut/HackerRank/blob/master/LICENSE)
@@ -223,6 +223,7 @@ platform tests of a given problem.
223223
|Trees|[Binary Search Tree : Lowest Common Ancestor](https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor)|Easy|30|[Solution.java](Data%20Structures/Trees/Binary%20Search%20Tree%20:%20Lowest%20Common%20Ancestor/Solution.java)|
224224
|Trees|[Swap Nodes [Algo]](https://www.hackerrank.com/challenges/swap-nodes-algo)|Medium|40|[Solution.java](Data%20Structures/Trees/Swap%20Nodes%20[Algo]/Solution.java)|
225225
|Trees|[Is This a Binary Search Tree?](https://www.hackerrank.com/challenges/is-binary-search-tree)|Medium|30|[Solution.cpp](Data%20Structures/Trees/Is%20This%20a%20Binary%20Search%20Tree?/Solution.cpp)|
226+
|Balanced Trees|[Self Balancing Tree](https://www.hackerrank.com/challenges/self-balancing-tree)|Medium|50|[Solution.java](Data%20Structures/Balanced%20Trees/Self%20Balancing%20Tree/Solution.java)|
226227
|Stacks|[Maximum Element](https://www.hackerrank.com/challenges/maximum-element)|Easy|20|[Solution.java](Data%20Structures/Stacks/Maximum%20Element/Solution.java)|
227228
|Stacks|[Balanced Brackets](https://www.hackerrank.com/challenges/balanced-brackets)|Medium|25|[Solution.java](Data%20Structures/Stacks/Balanced%20Brackets/Solution.java)|
228229
|Stacks|[Equal Stacks](https://www.hackerrank.com/challenges/equal-stacks)|Easy|25|[Solution.java](Data%20Structures/Stacks/Equal%20Stacks/Solution.java)|

0 commit comments

Comments
 (0)