#include<iostream>
using namespace std;
typedef int keyType;
typedef struct Node
{
keyType key;
Node *lchild, *rchild;
} * BTtree;
// search x in tree
Node* search(BTtree bt, keyType x)
{
if (bt == NULL || bt->key == x)
return bt;
else if (bt->key > x)
return search(bt->lchild, x);
else if (bt->key < x)
return search(bt->rchild, x);
}
// insert node p in tree
void insert(BTtree &bt, Node *p)
{
if (bt == NULL)
bt = p;
else if (bt->key > p->key)
insert(bt->lchild, p);
else if (bt->key < p->key)
insert(bt->rchild, p);
}
// creat bttree
BTtree creatBttree(int n)
{
BTtree bt = NULL;
for (int i = 0; i < n; i++)
{
keyType x;
cin >> x;
Node *s = (Node*)malloc(sizeof(Node));
s->key = x;
s->lchild = NULL;
s->rchild = NULL;
insert(bt, s);
}
return bt;
}
void show(BTtree bt)
{
if (bt != NULL)
{
show(bt->lchild);
cout << bt->key << ends;
show(bt->rchild);
}
}
int main()
{
BTtree p = creatBttree(5);
show(p);
cout << endl;
system("pause");
return 0;
}
二叉排序树的简单实现
最新推荐文章于 2017-08-11 18:49:49 发布