-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.cpp
142 lines (130 loc) · 3.23 KB
/
student.cpp
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
STUDENT_TREE* new_student_node(STUDENT* key){
STUDENT_TREE* node=new STUDENT_TREE;
node->s_key=key;
node->left=NULL;
node->right=NULL;
node->height=1;
return node;
}
int stu_tree_height(STUDENT_TREE* node)
{
if(node!=NULL)
return node->height;
return 0;
}
int balance_stu_tree(STUDENT_TREE* node)
{
if (node==NULL)
return 0;
return stu_tree_height(node->left)-stu_tree_height(node->right);
}
//rotations to balance the avl tree
STUDENT_TREE* llrotate_stu_rec(STUDENT_TREE* node)
{
STUDENT_TREE* temp=node->left;
node->left=temp->right;
temp->right=node;
node->height=max(stu_tree_height(node->left),stu_tree_height(node->right))+1;
temp->height=max(stu_tree_height(temp->left),stu_tree_height(temp->right))+1;
return temp;
}
STUDENT_TREE* rrrotate_stu_rec(STUDENT_TREE* node)
{
STUDENT_TREE* temp=node->right;
node->right=temp->left;
temp->left=node;
node->height=max(stu_tree_height(node->left),stu_tree_height(node->right))+1;
temp->height=max(stu_tree_height(temp->left),stu_tree_height(temp->right))+1;
return temp;
}
STUDENT_TREE* lrrotate_stu_rec(STUDENT_TREE* node)
{
STUDENT_TREE* temp=node->left;
node->left=rrrotate_stu_rec(temp);
return llrotate_stu_rec(node);
}
STUDENT_TREE* rlrotate_stu_rec(STUDENT_TREE* node)
{
STUDENT_TREE* temp=node->right;
node->right= llrotate_stu_rec(temp);
return rrrotate_stu_rec(node);
}
STUDENT_TREE* insert_stu_node(STUDENT_TREE* root,STUDENT* value)
{
if(root==NULL)
{
root=new_student_node(value);
return root;
}
if(value->stu_id==root->s_key->stu_id)
{
stu_dup_flag=1;
cout<<"Cannot insert the record with duplicate id"<<endl;
return root;
}
if(value->stu_id<root->s_key->stu_id)
{
root->left=insert_stu_node(root->left,value);
root->height=max(stu_tree_height(root->left),stu_tree_height(root->right))+1;
}
else{
root->right=insert_stu_node(root->right,value);
root->height=max(stu_tree_height(root->left),stu_tree_height(root->right))+1;
}
root=balance_student_tree(root);
return root;
}
STUDENT_TREE* balance_student_tree(STUDENT_TREE* root)
{
if(root==NULL)
return root;
if(balance_stu_tree(root)>1)
{
if(balance_stu_tree(root->left)>0)
{
root= llrotate_stu_rec(root);
}
else
{
root=lrrotate_stu_rec(root);
}
}
else if(balance_stu_tree(root)<-1)
{
if(balance_stu_tree(root->right)>0)
{
root=rlrotate_stu_rec(root);
}
else
{
root=rrrotate_stu_rec(root);
}
}
return root;
}
void show_students(STUDENT_TREE* root)
{
if(root==NULL)
{
return;
}
show_students(root->left);
cout<<root->s_key->stu_id<<"-"<<root->s_key->stu_name<<endl;
show_students(root->right);
return;
}
STUDENT_TREE* inorder_succ(STUDENT_TREE* node)
{
if(node->left==NULL)
return node;
return inorder_succ(node->left);
}
STUDENT_TREE* delete_leaf_node(STUDENT_TREE* root,STUDENT_TREE* node)
{
if(root==node)
{
return NULL;
}
root->left=delete_leaf_node(root->left,node);
return root;
}