-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path二叉查找树.cpp
More file actions
57 lines (56 loc) · 1.5 KB
/
二叉查找树.cpp
File metadata and controls
57 lines (56 loc) · 1.5 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
#include<bits/stdc++.h>
using namespace std;
typedef struct BiNode
{
int data; //结点的值,假设查找集合的元素为整型
BiNode *lchild, *rchild; //指向左、右子树的指针
} BiNode;
BiNode * SearchBST(BiNode *root, int k);
BiNode * insertBST(BiNode *tree,int data);
BiNode * createBST(int a[],int n);
int main()
{
BiNode *root, *t;
int a[]= {55,42,10,70,63,58,83,67,90,45};
root=createBST(a,10);
t=SearchBST(root,58);
if(t != NULL)
cout<<"查找成功"<<endl;
else
cout<<"查找失败"<<endl;
return 0;
}
BiNode *insertBST(BiNode *root, int data)
{
if(root == NULL)
{
root = new BiNode;
root->data = data;
root->lchild = NULL;
root->rchild = NULL;
return root;
}
if(data <= root->data)
root->lchild = insertBST(root->lchild, data);
else
root->rchild = insertBST(root->rchild, data);
return root;
}
BiNode *createBST(int a[], int n)
{
BiNode *T = NULL;
for (int i=0; i < n; i++)
T = insertBST(T, a[i]);
return T;
}
BiNode * SearchBST(BiNode *root, int k)
{
if (root == NULL)
return NULL; //二叉查找树为空,查找失败
else if (root->data == k)
return root; //查找成功
else if (k < root->data) //查找左子树
return SearchBST(root->lchild, k);
else //查找右子树
return SearchBST(root->rchild, k);
}