生成二叉查找树 中序遍历 输出结果 C++实现

依次输入表(30, 15, 28, 20, 24, 10, 12, 68, 35, 50, 46, 55)中的元素,生成一棵二叉排序数,

要求:编程构建一个二叉排序数,并中根遍历验证上述结果。

#include <stdio.h>
#include <iostream>
using namespace std;

#define size 20
typedef int Elemtype;

/*二叉排序树的二叉链表存储结构*/
typedef struct BTNode {
	Elemtype key;
	struct BTNode *lchild, *rchild;
}*BSTree;

/*二叉排序树的插入*/
BSTree InsertNode(BSTree T, BTNode *s) {
	if (T == NULL)
		return T = s;
	if (s->key < T->key)
		T->lchild = InsertNode(T->lchild, s);
	if (s->key > T->key)
		T->rchild = InsertNode(T->rchild, s);
	return T;
}
/*二叉排序树的创建*/
BSTree Create(BSTree T) {
	BSTree s = NULL;

	int key=0;
	printf("Please enter keywords,end with -1!\n");
	while (1) {
		scanf("%d", &key);
		if (key == -1)
			break;
		s = new BTNode;
		s->key = key;
		s->lchild = s->rchild = NULL;
		T = InsertNode(T, s);		
	}
	return T;
}

void print(BSTree T) {
	if (T == NULL)
		return;
	print(T->lchild);
	printf("%d ", T->key);
	print(T->rchild);
	return;
}

int main()
{
	BSTree T = NULL;
	T=Create(T);
	print(T);
	return 0;
}

《生成二叉查找树 中序遍历 输出结果 C++实现》

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