8607 实现二叉排序树的各种算法(1)
时间限制:1000MS 代码长度限制:10KB
提交次数:2573 通过次数:1472
题型: 编程题 语言: G++;GCC
Description
用函数实现如下二叉排序树算法:
(1) 插入新结点
(2) 前序、中序、后序遍历二叉树
(3) 中序遍历的非递归算法
(4) 层次遍历二叉树
(5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0)
输入格式
第一行:准备建树的结点个数n
第二行:输入n个整数,用空格分隔
第三行:输入待查找的关键字
第四行:输入待查找的关键字
第五行:输入待插入的关键字
输出格式
第一行:二叉树的先序遍历序列
第二行:二叉树的中序遍历序列
第三行:二叉树的后序遍历序列
第四行:查找结果
第五行:查找结果
第六行~第八行:插入新结点后的二叉树的先、中、序遍历序列
第九行:插入新结点后的二叉树的中序遍历序列(非递归算法)
第十行:插入新结点后的二叉树的层次遍历序列
输入样例
7
40 20 60 18 50 56 90
18
35
30
输出样例
40 20 18 60 50 56 90
18 20 40 50 56 60 90
18 20 56 50 90 60 40
1
0
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
18 20 30 40 50 56 60 90
40 20 60 18 30 50 90 56
作者 yqm
Version: 0
/代码片如下/
#include <iostream>
using namespace std;
int a[200];
typedef struct node //二叉排序树结构体
{
int data;
struct node *lchild, *rchild;
} node, *bst;
typedef struct //队列结构体
{
bst *base;
int head;
int tail;
} queue;
typedef struct
{
bst *base;
bst *top;
int stacksize;
} stack;
int initialstack(stack &s)
{
s.base = new bst[200];
if (s.base == NULL)
return 0;
s.stacksize = 200;
s.top = s.base;
return 1;
}
int push(stack &s, bst e)
{
if (s.top - s.base == s.stacksize)
return 0;
*s.top++ = e;
return 1;
}
int pop(stack &s, bst &e)
{
if (s.top == s.base)
return 0;
e = *--s.top;
return 1;
}
int initialqueue(queue &q) //队列初始化
{
q.base = new bst[200];
if (q.base == NULL)
return 0;
q.head = q.tail = 0;
return 1;
}
int in(queue &q, bst e) //队列入队
{
if ((q.tail + 1) % 200 == q.head)
return 0;
q.base[q.tail] = e;
q.tail = (q.tail + 1) % 200;
return 1;
}
int out(queue &q, bst &e) //队列出队
{
if (q.head == q.tail)
return 0;
e = q.base[q.head];
q.head = (q.head + 1) % 200;
return 1;
}
void ins(bst &t, int n) //二叉排序树插入
{
if (t == NULL)
{
t = new node;
t->data = n;
t->lchild = t->rchild = NULL; //cout<<t->data;
return;
}
else if (n < t->data)
return ins(t->lchild, n);
else if (n > t->data)
return ins(t->rchild, n);
}
void prego(bst t) //先序遍历
{
if (t)
{
cout << t->data << " ";
prego(t->lchild);
prego(t->rchild);
}
}
void midgo(bst t) //中序遍历
{
if (t)
{
midgo(t->lchild);
cout << t->data << " ";
midgo(t->rchild);
}
}
void reargo(bst t) //后序遍历
{
if (t)
{
reargo(t->lchild);
reargo(t->rchild);
cout << t->data << " ";
}
}
int search(bst t, int n) //利用二叉排序树二分查找
{
if (t == NULL)
return 0;
else
{
if (n == t->data)
return 1;
else if (n > t->data)
return search(t->rchild, n);
else if (n < t->data)
return search(t->lchild, n);
}
}
void levelgo(bst t, queue &q) //层次遍历
{
in(q, t);
while (q.head != q.tail)
{
bst e;
out(q, e);
cout << e->data << " ";
if (e->lchild != NULL)
in(q, e->lchild);
if (e->rchild != NULL)
in(q, e->rchild);
}
cout << endl;
}
void norecurmid(bst t, stack &s) //非递归中序遍历
{
while (t != NULL || s.base != s.top)
{ //只有遍历完并且栈空了才结束,单独一个栈为空是不够的,因为一开始就是空栈,单独一个t为NULL也是不够的,因为这只代表你遍历完一条分支,此时栈是非空的
if (t)
{
push(s, t);
t = t->lchild;
}
else
{
pop(s, t);
cout << t->data << " ";
t = t->rchild;
}
}
cout << endl;
}
int main()
{
bst t = NULL;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
ins(t, a[i]);
}
prego(t);
cout << endl;
midgo(t);
cout << endl;
reargo(t);
cout << endl;
int m;
cin >> m;
cout << search(t, m) << endl;
cin >> m;
cout << search(t, m) << endl;
int cha;
cin >> cha;
ins(t, cha);
prego(t);
cout << endl;
midgo(t);
cout << endl;
reargo(t);
cout << endl;
stack s;
initialstack(s);
norecurmid(t, s);
queue q;
initialqueue(q);
levelgo(t, q);
return 0;
}
/测试一下/
/测试通过/