-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvlTree.java
More file actions
142 lines (128 loc) · 4.19 KB
/
AvlTree.java
File metadata and controls
142 lines (128 loc) · 4.19 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
class BinaryNode{
Customer cstmr;
int height;
BinaryNode left;
BinaryNode right;
public BinaryNode(Customer data){
cstmr = data;
left = null;
right = null;
height = 0;
}
}
/**
* AVlTree
*/
public class AvlTree {
BinaryNode root;
public AvlTree(){
root = null;
}
protected int size(BinaryNode root){
if (root==null) {
return 0;
}
return 1 + size(root.left) + size(root.right);
}
private BinaryNode searchRecursive(BinaryNode root,int cstmrID){
if (root == null || cstmrID==root.cstmr.ID) {
return root;
}
else if (cstmrID < root.cstmr.ID) {
return searchRecursive(root.left, cstmrID);
}
else {
return searchRecursive(root.right, cstmrID);
}
}
public Customer search(int cstmrID){
BinaryNode search_result = searchRecursive(root,cstmrID);
if (search_result==null) {
return null;
}
else return searchRecursive(root,cstmrID).cstmr;
}
// Helper functions
private int getHeight(BinaryNode root){
if (root==null) {
return -1;
}
else {
return root.height;
}
}
private BinaryNode clockwise(BinaryNode root){
BinaryNode var;
var = root.left;
root.left = var.right;
var.right = root;
root.height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
var.height = 1 + Math.max(getHeight(var.left),getHeight(var.right));
return var;
}
private BinaryNode anticlockwise(BinaryNode root){
BinaryNode var;
var = root;
root = var.right;
var.right = root.left;
root.left = var;
var.height = 1 + Math.max(getHeight(var.left),getHeight(var.right));
root.height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
return root;
}
private BinaryNode insertRecursive(BinaryNode root, Customer cstmr){
if (root==null) {
root = new BinaryNode(cstmr);
return root;
}
if (root.cstmr.ID > cstmr.ID) root.left = insertRecursive(root.left, cstmr);
else if (root.cstmr.ID < cstmr.ID) root.right= insertRecursive(root.right, cstmr);
else return root;;
root.height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
///// Rotations Now /////
if (getHeight(root.left) - getHeight(root.right) > 1) { // Left - X case
if (cstmr.ID < root.left.cstmr.ID) { // Left - Left case
// Clockwise Root
root = clockwise(root);
}
else { // Left - Right case
// Anti-clockwise Root.left
root.left = anticlockwise(root.left);
// Clockwise Root
root = clockwise(root);
}
}
else if (getHeight(root.right) - getHeight(root.left) > 1){ // Right - X case
if (cstmr.ID > root.right.cstmr.ID) { // Right - Right case
// Anti-clockwise Root
root = anticlockwise(root);
}
else { // Right - Left case
// Clockwise Root.right
root.right = clockwise(root.right);
root = anticlockwise(root);
}
}
return root;
}
void insertCustomer(Customer cstmr){
root = insertRecursive(root, cstmr);
}
/*
public void printCustomerDetails(int ID){
System.out.println("----------");
Customer cstmr = this.search(ID);
if (cstmr==null){
System.out.print("TCustomer NOT found");
}
System.out.println("ID = " + cstmr.ID);
System.out.println("Counter = " + cstmr.counter);
System.out.println("Numb = " + cstmr.numb);
System.out.println("State = " + cstmr.state);
System.out.println("Arrival Time = " + cstmr.arrival_t);
System.out.println("Wait Time = " + cstmr.wait_time);
System.out.println("Burgers Waiting = " + cstmr.n_b_waiting);
System.out.println("Burgers Completed = " + cstmr.n_b_delivered);
}
*/
}