@【数据结构】(二叉树的定义及基本操作函数2-链式存储)

@【数据结构】(二叉树的定义及基本操作函数2)

采用链式结构存放二叉树,实现二叉树的创建,实现二叉树的遍历(前序,后序,中序层次遍历),分别求二叉树的叶子结点和结点的数目,二叉树的查找,二叉树的深度。

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

#define MAX 100

typedef char Elemtype;
typedef struct tNode
{
	Elemtype data;
	struct tNode *lchild, *rchild;
}BiTNode, *BiTree;
BiTNode *createtree()
{
	BiTNode *T ;
	char str;
	str = getchar();
	if (str == '#')
		return NULL;
	else
	{
		T = (BiTNode*)malloc(sizeof(BiTNode));
		T->data = str;
		T->lchild = createtree();
		T->rchild = createtree();
		return T;
	}
	
}
void preorder(BiTNode *bt)   %前序遍历
{
	if (bt == NULL)
		return;
	cout << bt->data << "   ";
	preorder(bt->lchild);
	preorder(bt->rchild);
}
void inorder(BiTNode *bt) %中序遍历
{
	if (bt == NULL)
		return;
	inorder(bt->lchild);
	cout << bt->data << "   ";
	inorder(bt->rchild);

}
void postorder(BiTree bt)  %后序遍历
{
	if (bt == NULL)
		return;
	postorder(bt->lchild);
	postorder(bt->rchild);
	cout << bt->data << "   ";
}
void levelorder(BiTNode *bt)  %层级遍历
{
	BiTNode *p;
	BiTNode *q[MAX];
	int front, rear;
	front = rear = 0;       //置队空
	rear++; q[rear] = bt;  //根节点指针入队
	while (front != rear)
	{
		front = (front + 1) % MAX;
		p = q[front];    //出队结点p
		cout << p->data << "   ";
		if (p->lchild != NULL)  // 有左孩子时入队
		{
			rear = (rear + 1) % MAX;
			q[rear] = p->lchild;
		}
		if (p->rchild != NULL)   //有右孩子时进队
		{
			rear = (rear + 1) % MAX;
			q[rear] = p->rchild;
		}
	}
}
BiTree search(BiTNode *bt, Elemtype x)   %查找
{
	BiTree p;
	p = bt;
	if (p->data == x)
		return p;
	if (p->lchild != NULL)
		return(search(p->lchild, x));
	if (p->rchild != NULL)
		return(search(p->rchild, x));
	return NULL;
}
int countall(BiTNode *bt)
{
	int l, r;
	if (bt == NULL)
		return(0);
	l = countall(bt->lchild);
	r = countall(bt->rchild);
	return(l + r + 1);
}
int countleaf(BiTNode *bt)   %计算叶子节点数
{
	if (bt == NULL)
		return(0);
	else if (bt->lchild == NULL && bt->lchild == NULL)
		return(1);
	else
		return(countleaf(bt->lchild) + countleaf(bt->rchild));
}
int treehigh(BiTNode *bt)   %求树深
{
	int rh, lh, h;
	if (bt == NULL) h=0;
	else
	{
		lh = treehigh(bt->lchild);
		rh = treehigh(bt->rchild);
		h=(lh > rh ? lh : rh) + 1;
	}
	return h;
}
void main()
{
	cout << "创建二叉树" << endl;
	cout << "请以先序序列输入节点的值,为空时输入'#':" << endl;
	BiTNode *bt= createtree();

	while (1)
	{
		cout << "请选择要进行的操作:" << endl;
		cout << "-------------------------------------" << endl;
		cout << "1.二叉树的前序遍历" << endl;
		cout << "2.二叉树的中序遍历" << endl;
		cout << "3.二叉树的后序遍历" << endl;
		cout << "4.二叉树的层次遍历" << endl;
		cout << "5.求二叉树的叶子节点及节点数" << endl;
		cout << "6.二叉树的查找" << endl;
		cout << "7.求二叉树的深度" << endl;
		int index;
		cin>>index;

		switch (index)
		{
		case 1: preorder(bt); cout << endl; break;
		case 2: inorder(bt); cout << endl; break;
		case 3:	postorder(bt);  cout << endl; break;
		case 4: levelorder(bt);  cout << endl; break;
		case 5: cout << "叶子结点数目为:" << countleaf(bt) << "    结点数目为:" << countall(bt) << endl; break;
		case 6:
		{
			Elemtype x;
			cout << "请输入要查找的元素值:";
			cin >> x;
			if (search(bt, x) == NULL)
				cout << "查无此数";
			cout << "该二叉树中已查到数:"<<x<<endl; break;
		}
		case 7: 
			cout <<"二叉树深度为:"<<treehigh(bt)<<endl; break;
		}
	}
	system("pause");
}

测试二叉树
在这里插入图片描述

(1)二叉树的创建(先序输入)
在这里插入图片描述
(2)遍历:前序+中序+后序+层级
前序遍历:先根节点,再左节点,右节点
在这里插入图片描述
中序遍历:先左节点,后根节点,最后右节点
在这里插入图片描述
后序遍历:左节点,右节点,最后根节点
在这里插入图片描述
在这里插入图片描述
(3)计算节点数及叶子节点数
在这里插入图片描述
(4)查找元素:
这里只能查找到元素是否存在,若要输出元素位置,需进一步完善代码
在这里插入图片描述
(5)树深
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值