二叉树(2)之二叉树的基本操作(遍历,找节点个数)

二叉树(2)之二叉树的基本操作(遍历,找节点个数)

最难的也就是层次遍历:各种遍历函数其实就是各种递归。叶子节点,深度,总节点,掌握性质都不难‘’

层次遍历

层次遍历,就是从上到下一层一层的遍历 
例如:这里写图片描述


思路:

 

 

这里写图片描述

上代码:层次遍历暂时没用真正的队列不够原理上是一样的。(以后能力提高了在搞)

#include<algorithm> 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> 
using namespace std;
typedef struct bitnode//处理前面和这有关后面基本上和这无关 
{
	char  data;//shurudshuju
    struct	bitnode *lchild, *rchild; //houzaohanshu
}bitnode, *bittree;
void create(bittree &t)
{
	char c;
	cin >> c;
	if(c=='#')
		t = NULL ;
	else 
	{
		t=new bitnode;
		t->data = c;	
		create(t->lchild);
		create(t->rchild);
	 } 
}
void xianxu(bittree t)
{
	if(t==NULL)
		return ;
	else
	{
		cout<<t->data;
		xianxu(t->lchild);
		xianxu(t->rchild);
	}
}
void zhongxu(bittree t)
{
	if(t==NULL)
		return  ;
	else
	{
		zhongxu(t->lchild);
		cout<<t->data;
		zhongxu(t->rchild);
	} 
}
void houxu(bittree t)
{
	if(t==NULL)
		return ;
	else 
	{
		houxu(t->lchild);
		houxu(t->rchild);
		cout<<t->data;
	}
}
int deep(bittree t)
{
	int d1=0,d2=0;
	if(t==NULL)
		return 0;
	else 
	{
		d1=deep(t->lchild)+1;
		d2=deep(t->rchild)+1;
	}
	return d1>=d2?d1:d2;
}
//yezhishu
int yezhi(bittree t)
{
	if(t==NULL)
	return 0;
	if(t->lchild==NULL&&t->rchild==NULL)
	return 1;
	else 
	{
		return yezhi(t->lchild) + yezhi(t->rchild);
	}
}
int jiedian(bittree t)
{
	if(t == NULL)
	return 0;
	else
		return jiedian(t->lchild)+jiedian(t->rchild)+1;
}
void cengci(bittree t)
{
	bittree q;
	bittree queue[10];
	int front = 0, rear = 0;
	if(t == NULL)
		return ;
	else 
	{
		rear = (rear+1)%10;
		queue[rear]=t;
		while(front!=rear)
		{
			front=(front+1)%10;
			q=queue[front];
			cout<<q->data;
			if(q->lchild!=NULL)
			{
				rear=(rear+1)%10;
				queue[rear]=q->lchild;
			}
			if(q->rchild!=NULL)
			{
				rear = (rear+1)%10;
				queue[rear]=q->rchild;
			}
		}
	}
} 
int main()
{
	bittree T;
	cout<<"jianshu:"<<endl;
	create(T);
	cout<<endl;
	cout<<"qianxu:"<<endl;
	xianxu(T);
	cout<<endl;
	cout<<"zhongxu"<<endl;
	zhongxu(T);
	cout<<endl;
	cout<<"houxu"<<endl;
	houxu(T);
	cout<<endl;
	cout<<yezhi(T)<<" "<<jiedian(T)<<"  "<<deep(T)<<endl;
	cengci(T);
	return 0;
}
//ABC##DE#G##F###

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114882.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【AVD】简述某些视频在线播放时卡顿、本地播放时不卡顿的问题

    【AVD】简述某些视频在线播放时卡顿、本地播放时不卡顿的问题曾经在业务中遇到过这样的问题,我们编码出来的视频在Android、iOS端,使用ijkplayer内核的播放器播放时卡顿,甚至无法任意定位播放位置,将导致卡顿无法播放。今天,又有同事遇到类似的问题,而我发现,我只写过一个《用notepad++和Excel协助分析媒体文件包》,而并没有把当时遇到的问题分析记录下来。于是,在此简单说明一下。视频文件结构教科书般的教程、课程中对视频文件结构的描述非常详细,此处不赘述,简单地说,视频文件也是一种文件,是文件,就是一堆二进制数的集合,而且是一个.

    2025年11月25日
    3
  • Best Time to Buy and Sell Stock II

    Best Time to Buy and Sell Stock II

    2022年1月25日
    54
  • div滚动条联动「建议收藏」

    div滚动条联动「建议收藏」应用场景:一般是表头和表体,或者是需要联动的div,滚动一个div的滚动条,让另一个div的滚动条也随之滚动到一样的位置//表头<divid=”sc-table-head-div”class=”sc-table-head-div”style=”position:absolute;”> <tableid=”conitor_head”style=”…

    2022年7月12日
    26
  • 自定义url是什么意思_application.getbean

    自定义url是什么意思_application.getbeanSpringBoot简洁工程生成:https://start.spring.io/默认生成样式:@SpringBootApplicationpublicclassSpringParentApplication{publicstaticvoidmain(String[]args){SpringApplicatio…

    2025年10月17日
    3
  • 使用R中merge()函数合并数据[通俗易懂]

    使用R中merge()函数合并数据[通俗易懂]使用R中merge()函数合并数据在R中可以使用merge()函数去合并数据框,其强大之处在于在两个不同的数据框中标识共同的列或行。如何使用merge()获取数据集中交叉部分merge()最简单的形式为获取两个不同数据框中交叉部分。举例,获取cold.states和large.states完全匹配的数据。代码如下:&gt;merge(cold.states,large….

    2022年6月14日
    52
  • HTML+CSS美食静态网页设计

    HTML+CSS美食静态网页设计爱尚美食网页用AdobeDreamweaverCS6制作代码如下:HTML代码:<!DOCTYPEhtml><html><head><metacharset=utf-8″/><title>爱尚美食</title><!–链接图片及css–><linkrel=”…

    2022年9月23日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号