- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAVL.cpp
More file actions
Latest commit
151 lines (137 loc) · 3.25 KB
/
Copy pathAVL.cpp
File metadata and controls
151 lines (137 loc) · 3.25 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
AVLTREEINSERTION
classSolution{
public:
/*You are required to complete this method */
intheight(Node*root){
if(root==NULL){
return0;
}
return root->height;
}
intbf(Node*root){
if(root==NULL)return0;
returnheight(root->left)-height(root->right);
}
Node*right(Node*root){
Node*x=root->left;
Node*T2=x->right;
root->left=T2;
x->right=root;
root->height=max(height(root->left),height(root->right))+1;
x->height=max(height(x->left),height(x->right))+1;
return x;
}
Node*left(Node*root){
Node*y=root->right;
Node*T2=y->left;
root->right=T2;
y->left=root;
root->height=max(height(root->left),height(root->right))+1;
y->height=max(height(y->left),height(y->right))+1;
return y;
}
Node*create(int k){
Node*temp=newNode(k);
temp->left=NULL;
temp->right=NULL;
temp->height=1;
return temp;
}
Node* insertToAVL(Node* root, int data)
{
if(root==NULL)returncreate(data);
if(root->data>data){
root->left=insertToAVL(root->left,data);
}
elseif(root->data<data){
root->right=insertToAVL(root->right,data);
}
else{
return root;
}
root->height=max(height(root->left),height(root->right))+1;
int bal = bf(root);
if(bal>1 && root->left->data>data){
returnright(root);
}
if(bal<-1 && root->right->data<data){
returnleft(root);
}
if(bal>1 && root->left->data<data){
root->left=left(root->left);
returnright(root);
}
if(bal<-1 && root->right->data>data){
root->right=right(root->right);
returnleft(root);
}
return root;
//Your code here
}
};
/*
AVL Tree Deletion
Hard Accuracy: 37.18% Submissions: 6161 Points: 8
Given a AVL tree and N values to be deleted from the tree. Write a function to delete a given value from the tree.
Example 1:
Tree =
4
/ \
2 6
/ \ / \
1 3 5 7
N = 4
Values to be deleted = {4,1,3,6}
Input: Value to be deleted = 4
Output:
5
/ \
2 6
/ \ \
1 3 7
Input: Value to be deleted = 1
Output:
5
/ \
2 6
\ \
3 7
Input: Value to be deleted = 3
Output:
5
/ \
2 6
\
7
Input: Value to be deleted = 6
Output:
5
/ \
2 7
*/
voidinorder(Node*root,vector<int>&v, int data){
if(root==NULL){
return;
}
inorder(root->left,v,data);
if(root->data!=data){
v.push_back(root->data);
}
inorder(root->right,v,data);
}
Node*build(vector<int>v,int start,int end){
if(start>end){
returnNULL;
}
int mid=(start+end)/2;
Node*temp=newNode(v[mid]);
temp->left=build(v,start,mid-1);
temp->right=build(v,mid+1,end);
return temp;
}
Node* deleteNode(Node* root, int data)
{
vector<int>v;
inorder(root,v,data);
returnbuild(v,0,v.size()-1);
}