-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST
111 lines (77 loc) · 1.47 KB
/
BST
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
#include<bits/stdc++.h>
using namespace std;
struct Node
{
struct Node *lchild;
struct Node *rchild;
int data;
};
struct Node *Insert(struct Node *p,int key)
{
struct Node *t;
if(p==NULL)
{
t=(struct Node *)malloc(sizeof(struct Node));
t->data=key;
t->lchild=t->rchild=NULL;
return t;
}
if(p->data>key)
{
p->lchild=Insert(p->lchild,key);
}
else if(p->data<key)
p->rchild=Insert(p->rchild,key);
return p;
}
void Preorder(struct Node *p)
{
cout<<p->data;
Preorder(p->lchild);
Preorder(p->rchild);
}
void *Inorder(struct Node *p)
{
Inorder(p->lchild);
cout<<p->data;
Inorder(p->rchild);
}
void *Postorder(struct Node *p)
{
Postorder(p->lchild);
Postorder(p->rchild);
cout<<p->data;
}
struct Node *Search(struct Node *p,int key)
{
if(p->data==key)
return p;
else if(p->data>key)
{
Search(p->lchild,key);
}
else
Search(p->rchild,key);
return NULL;
}
struct Node Inpre(struct Node *p) //InorderPredessor
{
if(p&&p->lchild!=NULL)
p=p->lchild;
return p;
}
struct Node Insuc(struct Node *p) //InorderSuccesor
{
if(p&&p->rchild!=NULL)
p=p->rchild;
return p;
}
int height(struct Node *p)
{
int x,y;
if(p==NULL)
return 0;
x=height(p->lchild);
y=height(p->rchild);
return max(x,y)+1;
}