(2) 建立一棵二叉排序树,然后进行中序遍历,并实现动态查找。

(2) 建立一棵二叉排序树,然后进行中序遍历,并实现动态查找。(输入零为空)

头文件:1.h

#include <iostream>
#include <malloc.h>
#include <string.h>
#define TRUE  1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW    -2
#define OK 1
typedef int Status;
using namespace std;

头文件:2.h

typedef int TElemType;
typedef int KeyElemType;
typedef struct BiTNode
{
	TElemType data;
	BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

头文件:3.h

#include "2.h"
Status CreatBitree(BiTree &T)//先序建立二叉树
{
	TElemType ch;
	cin >> ch;
	if (ch == 0)
	{
		T = NULL;

	}
	else
	{
		T = (BiTNode *)malloc(sizeof(BiTNode));
		if (!T)
			return OVERFLOW;
		T->data = ch;
		CreatBitree(T->lchild);
		CreatBitree(T->rchild);
	}

	return OK;
}
BiTree SearchBST(BiTree T, KeyElemType key)
{
	if ((!T) || T->data == key) return T;
	else if (T->data < key) return SearchBST(T->rchild, key);
	else if (T->data > key) return SearchBST(T->lchild, key);
}

源程序

int main()
{
	BiTree T,D;
	cout << "请输入一个二叉排序树:(输入0位空)";
	CreatBitree(T);
	KeyElemType key;
	cout << "请输入查询的数目:";
	cin >> key;
	D = SearchBST(T, key);
	if (D == NULL) cout << "查询失败" << endl;
	else
		cout << D->data;
	system("pause");
	
}

《(2) 建立一棵二叉排序树,然后进行中序遍历,并实现动态查找。》
《(2) 建立一棵二叉排序树,然后进行中序遍历,并实现动态查找。》

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/GUAN_GUAN_A/article/details/84402439
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞