-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathAVLTree.java
111 lines (93 loc) · 2.35 KB
/
AVLTree.java
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
package com.deepak.data.structures.Tree;
/**
* An AVL tree is another self-balanced binary search tree.
* The heights of the two child subtrees of any node differ by at most one;
* otherwise, re balancing is done to restore this property.
* The AVL tree maintains an extra attribute in each node to balance the height.
* The heights of two child subtree (left subtree and
* right subtree) of any node differ by at most one
*
* @author Deepak
*/
public class AVLTree {
private AVLTreeNode root;
public boolean isEmpty() {
return root == null;
}
public void makeEmpty() {
root = null;
}
public void insert(int data) {
root = insert(data, root);
}
private AVLTreeNode insert(int data, AVLTreeNode node) {
if (node == null) {
root = new AVLTreeNode(data);
} else if (data < node.data) {
node.left = insert(data, node.left);
if (height(node.left) - height(node.right) == 2) {
if (data < node.left.data) {
node = rotateLeft(node);
} else {
node = doubleRotateLeft(node);
}
}
} else if (data > node.data) {
node.right = insert(data, node.right);
if (height(node.right) - height(node.left) == 2) {
if (data > node.right.data) {
node = rotateRight(node);
} else {
node = doubleRotateRight(node);
}
}
}
node.height = max(height(node.left), height(node.right)) + 1;
return node;
}
private int max(int lhs, int rhs) {
return lhs > rhs ? lhs : rhs;
}
private AVLTreeNode rotateLeft(AVLTreeNode node) {
// TODO Auto-generated method stub
return null;
}
private AVLTreeNode doubleRotateLeft(AVLTreeNode node) {
// TODO Auto-generated method stub
return null;
}
private AVLTreeNode rotateRight(AVLTreeNode node) {
// TODO Auto-generated method stub
return null;
}
private AVLTreeNode doubleRotateRight(AVLTreeNode node) {
// TODO Auto-generated method stub
return null;
}
public int height(AVLTreeNode node) {
return node == null ? -1 : node.height;
}
/**
* AVL Tree Node
*
* @author Deepak
*/
public class AVLTreeNode {
private AVLTreeNode left;
private AVLTreeNode right;
private int data;
private int height;
public AVLTreeNode() {
this.left = null;
this.right = null;
this.data = 0;
this.height = 0;
}
public AVLTreeNode(int data) {
this.left = null;
this.right = null;
this.data = data;
this.height = 0;
}
}
}