最近在复习数据结构,顺便看看大一的时候写的代码,看完之后比当初有了更加深刻的体会。
希望这些能提供给初学者一些参考。
在VC++6.0下可运行,当初还写了不少注释。
【问题描述】
以输入的一组整数作为关键字的值,构造其对应的二叉排序树,并对给点的值在该二叉排序树上进行查找
【基本要求】
·输入:输入一组关键字(整数)及要查找的值
·输出:排好序的关键字及查找的结果
样例输入
60 35 69 84 96 13 66 34 21 -1
40
样例输出
13 21 34 35 60 66 69 84 96
40…… 没找到
【模块划分】
1. BSTInOrder()函数:中序遍历二叉排序树
2. BTSInsert()函数:在二叉排序树上插入一个结点
3. CreateBST()函数:生成二叉排序树
4. BSTSearch(): 在二叉排序树上查找,找到,返回指向该节点的指针,否则返回空指针
5. main函数:调用各函数
#include <stdio.h>
#include <malloc.h>
typedef struct BSTNode
{
<span style="white-space:pre"> </span>int key;
<span style="white-space:pre"> </span>struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
BSTree f;
//中序遍历二叉排序树
void BSTInOrder(BSTree t)
{
<span style="white-space:pre"> </span>if(t)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>BSTInOrder(t->lchild);
<span style="white-space:pre"> </span>printf("%-4d ",t->key);
<span style="white-space:pre"> </span>BSTInOrder(t->rchild);
<span style="white-space:pre"> </span>}
}
//在二叉树中查找值为k的结点
int BSTSearch(BSTree t, int k)
{
<span style="white-space:pre"> </span>BSTree p;
<span style="white-space:pre"> </span>p=t;
<span style="white-space:pre"> </span>f=NULL;
<span style="white-space:pre"> </span>while(p)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(k==p->key)
<span style="white-space:pre"> </span>return 1;
<span style="white-space:pre"> </span>else if(k<p->key)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>f=p;
<span style="white-space:pre"> </span>p=p->lchild;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>f=p;
<span style="white-space:pre"> </span>p=p->rchild;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}
//在二叉树t中查找关键字值为k的结点,若找不到k,则插入k
void BSTInsert(BSTree& t, int k)
{
<span style="white-space:pre"> </span>BSTree s;
<span style="white-space:pre"> </span>int p;
<span style="white-space:pre"> </span>p=BSTSearch(t,k);
<span style="white-space:pre"> </span>if(!p)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>s=(BSTNode*)malloc(sizeof(BSTNode));
<span style="white-space:pre"> </span>s->key=k;
<span style="white-space:pre"> </span>s->lchild=s->rchild=NULL;
<span style="white-space:pre"> </span>if(!t)
<span style="white-space:pre"> </span>t=s;
<span style="white-space:pre"> </span>else if(k<f->key)
<span style="white-space:pre"> </span>f->lchild=s;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>f->rchild=s;
<span style="white-space:pre"> </span>}
}
//创建二叉排序树
BSTree CreateBST()
{
<span style="white-space:pre"> </span>BSTree t;
<span style="white-space:pre"> </span>int key;
<span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>BSTree BSTInsert(BSTree t, int k);
<span style="white-space:pre"> </span>t=NULL;
<span style="white-space:pre"> </span>printf("输入数据\n");
<span style="white-space:pre"> </span>scanf("%d",&key);
<span style="white-space:pre"> </span>while(key!=-1)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>BSTInsert(t, key);
//<span style="white-space:pre"> </span>printf("输入数据\n");
<span style="white-space:pre"> </span>scanf("%d",&key);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return t;
}
int main()
{
<span style="white-space:pre"> </span>BSTree root;
<span style="white-space:pre"> </span>int p,key;
<span style="white-space:pre"> </span>root=CreateBST();
<span style="white-space:pre"> </span>printf("二叉排序树列表\n");
<span style="white-space:pre"> </span>BSTInOrder(root);
<span style="white-space:pre"> </span>printf("\n输入要查找的值\n");
<span style="white-space:pre"> </span>scanf("%d",&key);
<span style="white-space:pre"> </span>p=BSTSearch(root,key);
<span style="white-space:pre"> </span>if(p)
<span style="white-space:pre"> </span>printf("%d...... 能够找到\n",key);
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>printf("%d...... 没找到\n",key);
<span style="white-space:pre"> </span>return 0;
}
原文地址:http://blog.csdn.net/u011694809/article/details/45675341