-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTparent.cpp
More file actions
60 lines (53 loc) · 1.17 KB
/
Copy pathBSTparent.cpp
File metadata and controls
60 lines (53 loc) · 1.17 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
//Parent pointer bst
#include<iostream>
struct NodeP
{
int data;
NodeP *left;
NodeP *right;
NodeP *parent;
};
NodeP* Insert(NodeP *root,char data) {
if(root == NULL) {
root = new NodeP();
root->data = data;
root->left = root->right = NULL;
root->parent=NULL;
}
else if(data <= root->data){
root->left = Insert(root->left,data);
root->left->parent=root;
}
else{
root->right = Insert(root->right,data);
root->right->parent=root;
}
return root;
}
struct NodeP* FindMin(struct NodeP* root) {
if(root == NULL) return NULL;
while(root->left != NULL)
root = root->left;
return root;
}
struct NodeP* Find(struct NodeP* root,int data) {
if(root == NULL) return NULL;
if(root->data==data)
return root;
else if(root->data>=data)
Find(root->left,data);
else if (root->data<data)
Find(root->right,data);
//return root;
}
int main()
{
NodeP* root = NULL;
root = Insert(root,8); root = Insert(root,10);
root = Insert(root,4); root = Insert(root,5);
root = Insert(root,1); root = Insert(root,11);
root= Insert(root,3);
NodeP *temp=Find(root,3);
std::cout<<temp->parent->data<<"\n";
return 0;
}