【二叉树】数据结构实验代码(二)


 

二叉树的叶子结点个数计算

  • 首先先序创建好整个二叉树
  • 再进行先序遍历的时候,当访问到孩子数为0的结点时,cnt增1
#include<iostream>
using namespace std;
int cnt;
struct btNode
{
	char data;
	btNode* lchild;
	btNode* rchild;
};
void CreatebtNode(btNode *&bt)
{
	char ch; cin >> ch;
	if (ch == '0')bt = NULL;
	else
	{
		bt = new btNode;
		bt->data = ch;
		cout << "输入" << bt->data << "的左节点:" << endl;
		CreatebtNode(bt->lchild);
		cout << "输入" << bt->data << "的右节点:" << endl;
		CreatebtNode(bt->rchild);
	}
}
void DLR(btNode*& bt)
{
	if (bt!= NULL)
	{
		if (bt->lchild == NULL && bt->rchild == NULL)
		{
			cout << bt->data << endl;
			cnt++;
		}
		else
		{
			DLR(bt->lchild);
			DLR(bt->rchild);
		}
	}
}
int main()
{
	btNode* bt;
	cout << "请输入根节点:" << endl;
	CreatebtNode(bt);
	DLR(bt);
	cout << "叶子结点数量为:" << cnt << endl;
}

二叉树相似判断

  • 先序创建整个二叉树
  • 再先序对比两个二叉树的形状,当遇到不同的结点,st更新为false
#include<iostream>
using namespace std;
struct bitNode
{
	char data;
	bitNode* lchild;
	bitNode* rchild;
};
	void Createtree(bitNode *&bt)
	{
		char ch; cin >> ch;
		if (ch == '0')bt = NULL;
		else
		{
			bt = new bitNode;
			bt->data = ch;
			cout << "输入" << bt->data << "的左节点:" << endl;
			Createtree(bt->lchild);
			cout << "输入" << bt->data << "的右节点:" << endl;
			Createtree(bt->rchild);
		}
	}
	bool st = true;
	void DLRcom(bitNode* bt1,bitNode*bt2)
	{
		if (bt1 == NULL && bt2 == NULL)
		return;
		else if(bt1 != NULL && bt2 != NULL)
		{
			DLRcom(bt1->lchild, bt2->lchild);
			DLRcom(bt1->rchild, bt2->rchild);
		}
		else
			st = false;
	}

int main()
{
	cout << "请输入树1的祖先结点,输入0为空" << endl;
	bitNode *t1, *t2;
	Createtree(t1);
	cout << "请输入树2的祖先结点,输入0为空" << endl;
	Createtree(t2);
	DLRcom(t1, t2);
	if (st)
	{
		cout << "相似" << endl;
	}
	else
	cout << "不相似" << endl;



	
}

二叉树结点特征计算 

  • 记录层数level,层次遍历,通过当前结点的双亲结点的层次parent->level加一更新
  • 路径长度与祖先个数相同,只需要在先序遍历时记录即可,当递归返回时,将anc--
  • 孩子个数需要通过后序遍历来记录
  • 更新等于当前结点的左孩子结点的孩子个数 lchild->child+1(自身)+rchild->child+1(自身)
#include<iostream>
#include<queue>
using namespace std;
struct Node
{
	char data;
	Node* lchild;
	Node* rchild;
	Node* parent = NULL;
	int anc;
	int child = 0;
	int level;
	int preorder;
	int inorder;
	int postorder;
	int levelorder;
};
int cnt, anc=-1, child;
void Createtree(Node *&t)//本质是DLR
{
	char ch; cin >> ch;
	if (ch == '0')
	{
		t = NULL;
		
	}
	else
	{
		t = new Node;
		t->data = ch;
		t->preorder = ++cnt;
		t->anc = ++anc;
		cout << "输入" << t->data << "的左节点:" << endl;
         Createtree(t->lchild);
		if (t->lchild)t->lchild->parent = t;
		cout << "输入" << t->data << "的右节点:" << endl;
		Createtree(t->rchild);
		if (t->rchild)t->rchild->parent = t;
		anc--;
	}
}
int levelcnt;
void level(Node*& t)
{
	queue<Node*> q;
	t->level = 1;
	q.push(t);
	while (!q.empty())
	{
		auto p = q.front(); q.pop();
		p->levelorder = ++levelcnt;
		if(p->parent)
		p->level = p->parent->level + 1;
		if (p->lchild)
			q.push(p->lchild);
		if (p->rchild)
			q.push(p->rchild);
	}
}
int inordercnt;
void LDR(Node*& t)
{
	if (t)
	{
		LDR(t->lchild);
		t->inorder = ++inordercnt;
		LDR(t->rchild);
	}
}
int postordercnt;
void LRD(Node*& t)
{
	if (t)
	{
		LRD(t->lchild);
		LRD(t->rchild);
		if (t->lchild)
		t->child += t->lchild ->child + 1;
		if (t->rchild)
		t->child += t->rchild->child + 1;
		t->postorder = ++postordercnt;
	}
}
void DLRsearch(char c, Node*& t)
{
	if (t)
	{
		if (t->data == c)
		{
			auto node = t;
			cout << "the level of the node is:" << node->level << endl;
			cout << "the length of the node is:" << node->anc << endl;
			cout << "the number of its child is:" << node->child << endl;
			cout << "the number og its ancestors is:" << node->anc << endl;
			cout << "the preorder of the node is:" << node->preorder << endl;
			cout << "the inorder of the node is:" << node->inorder << endl;
			cout << "the postorder of the node is:" << node->postorder << endl;

		}
		DLRsearch(c, t->lchild);
		DLRsearch(c, t->rchild);
	}
}
int main()
{
	Node* tree;
	puts("please input a tree by inputing its nodes one by one(-1 means NULL)");
	cout << "请输入根节点";
	Createtree(tree);
	level(tree);
	LDR(tree);
	LRD(tree);
	puts("输入一个结点的数据,这个结点的特征将会被输出:");
	char c;
	while (cin >> c)
	{
		DLRsearch(c, tree);
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nathan Qian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值