-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavltree.java
More file actions
152 lines (133 loc) · 4.33 KB
/
avltree.java
File metadata and controls
152 lines (133 loc) · 4.33 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
package practice;
import java.util.Scanner;
/**
* Created by linh on 16/05/2017.
*
*
AVL tree khi chèn vào cây làm cây mất cân bằng thì sẽ chia thành
4 Trường hợp:
- Trường hợp 1 phía trái trái không cân bằng thì sẽ quay phải
T1, T2, T3 và T4 là một nhánh con.
z y
/ \ / \
y T4 Right Rotate x z
/ \ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
- Trường hợp 2 phía trái phải không cần bằng thì sẽ quay trái phải
z x
/ \ / \
y T4 Left Right Rotate y z
/ \ - - - - - - - - -> / \ / \
T1 x T1 T2 T3 T4
/ \
T2 T3
- Trường hợp 3 phía phải không cần bằng thì sẽ quay trái
z y
/ \ / \
T1 y Left Rotate z x
/ \ - - - - - - - -> / \ / \
T2 x T1 T2 T3 T4
/ \
T3 T4
- Trường hợp 4 phía phải trái không cân bằng thì sẽ quay phải trái
z x
/ \ Right Left Rotate / \
T1 y - - - - - - - -> z y
/ \ / \ / \
x T4 T1 T2 T3 T4
/ \
T2 T3
*
*
*/
class Node {
public Node() {
super();
}
public Node(int data) {
this.data = data;
}
int data;
Node left;
Node right;
}
public class avltree {
Node root;
int height(Node node) {
return (node != null) ? Math.max(height(node.left), height(node.right)) + 1 : 0;
}
Node rightRotate(Node node){
Node nodeLeft = node.left;
node.left = nodeLeft.right;
nodeLeft.right = node;
return nodeLeft;
}
Node leftRightRotate(Node node){
Node nodeLeftRight = leftRotate(node.left);
nodeLeftRight = rightRotate(nodeLeftRight);
return nodeLeftRight;
}
Node leftRotate(Node node){
Node nodeRight = node.right;
node.right = nodeRight.left;
nodeRight.left = node;
return nodeRight;
}
Node rightLeftRotate(Node node){
Node nodeRightLeft = rightRotate(node.right);
nodeRightLeft = leftRotate(nodeRightLeft);
return nodeRightLeft;
}
Node insert(Node root, int data) {
if (root == null) {
root = new Node(data);
}
if (data == root.data) return root;
if (data > root.data)
root.right = insert(root.right, data);
else {
root.left = insert(root.left, data);
}
int heightOfNodeLeft = height(root.left);
int heightOfNodeRight = height(root.right);
if (Math.abs(heightOfNodeLeft - heightOfNodeRight) > 1) {
if (heightOfNodeLeft > heightOfNodeRight) {
int heighOfNodeLeftLeft = height(root.left.left);
int heighOfNodeLeftRight = height(root.left.right);
if (heighOfNodeLeftLeft > heighOfNodeLeftRight) root = rightRotate(root);
else root = leftRightRotate(root);
} else {
int heighOfNodeRightRight = height(root.right.right);
int heighOfNodeRightLeft = height(root.right.left);
if (heighOfNodeRightRight > heighOfNodeRightLeft) root = leftRotate(root);
else root = rightLeftRotate(root);
}
}
return root;
}
void insert(int data){
root = insert(root,data);
}
void print(Node root){
if (root != null){
print(root.left);
System.out.println(root.data);
print(root.right);
}
}
void print(){
print(root);
}
public static void main(String[] args) {
avltree s = new avltree();
Scanner sc = new Scanner(System.in);
int k = 0;
do{
k = sc.nextInt();
s.insert(k);
}while (k != 0);
s.print();
}
}