-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
139 lines (131 loc) · 2.64 KB
/
BST.cpp
File metadata and controls
139 lines (131 loc) · 2.64 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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
node* newNode(int data)
{
node* root= new node;
root->data=data;
root->left=NULL;
root->right=NULL;
}
node* minimal_tree(int arr[],int start, int end)
{
if(start>end)
return NULL;
int mid=(start+end)/2;
node* root = newNode(arr[mid]);
root->right=minimal_tree(arr,mid+1,end);
root->left=minimal_tree(arr,start,mid-1);
return root;
}
void preorder(node* root)
{
if(root==NULL)
return;
else
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
int height(int data);
bool isBalanced(node* root)
{
if(root==NULL)
return 1;
int lh;
int rh;
lh=height(root->left);
rh= height(root->right);
if((abs(lh-rh)<=1&&isBalanced(root->left)&&isBalanced(root->right))
return 1;
else return 0;
}
int height(node* root)
{
if(root==NULL)
return 1;
else
return 1+ max(height(root->left),height(root->right));
}
bool validateBST(node* root)
{
if(root==NULL)
return true;
else if(root->left->data>root->data||root->right->data<root->data)
return false;
else if(validateBST(root->right)&&validateBST(root->right));
return false;
}
node* successor(node* root, node* n)
{
if(n->right!=NULL)
return minValue(n->right);
node* p=n->parent;
while(p!=NULL&&n==p->right)
{
n=p;
p=p->parent;
}
return p;
}
node* minValue(node* n)
{
node* current=n;
while(current->left!=NULL)
current=current->left;
return current;
}
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(data));
else
{
struct node *temp;
/* 2. Otherwise, recur down the tree */
if (data <= node->data)
{
temp = insert(node->left, data);
node->left = temp;
temp->parent= node;
}
else
{
temp = insert(node->right, data);
node->right = temp;
temp->parent = node;
}
/* return the (unchanged) node pointer */
return node;
}
}
/* Driver program to test above functions*/
int main()
{
struct node* root = NULL, *temp, *succ, *min;
//creating the tree given in the above diagram
root = insert(root, 20);
root = insert(root, 8);
root = insert(root, 22);
root = insert(root, 4);
root = insert(root, 12);
root = insert(root, 10);
root = insert(root, 14);
temp = root->left->right->right;
succ = inOrderSuccessor(root, temp);
if(succ != NULL)
printf("\n Inorder Successor of %d is %d ", temp->data, succ->data);
else
printf("\n Inorder Successor doesn't exit");
getchar();
return 0;
}