-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAvl_tree.cpp
50 lines (42 loc) · 856 Bytes
/
Avl_tree.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
#include<iostream>
using namespace std;
struct Node{
Node *left;
Node *right;
int data;
Node(int val){
left = right =NULL;
data = val;
}
};
Node *insert(Node *root,int val){
if (root==NULL)
{
return new Node(val);
}
if(root->data<val){
root->right = insert(root->right,val);
}
else if(root->data>val){
root->left = insert(root->left,val);
}
return root;
}
// inorder Traversal
void inorder(Node *root){
if(root!=nullptr){
inorder(root->left);
cout<<root->data;
inorder(root->right);
}
}
int main(){
Node* root = new Node(50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
inorder(root);
}