简单二叉树创立(递归,适合新手)

本篇博客只简单创立一个二叉树,关于树的基本操作不做解释。

博主也只是刚学了树,怕误了大家,所以不多做赘述,如果内容有任何纰漏,还希望能指出来,互相学习。

1.关于二叉树
它是一种简单的树,它的定义和一些特性在网上或者是书上都可以找到。

2.创建

因为二叉树的特性,这里我们便可以用递归的方法一步一步把树的所有节点建立出来。这里使用的是孩纸链表,和线性链表的创立相似,不同的是二叉树有两个指针,一个指向左孩子,一个指向右孩子。在这里插入图片描述
首先创建一个根,利用递归的思想,创建左边的孩子,然后逐层返回。在这里插入图片描述
在这里插入图片描述
比如创建如图所示的二叉树,那么根据递归的思想,第一个创建的是根结点A,然后先从左边开始递归(看下面的代码,再来看这里),创建A的左孩子B,然后是B的左孩子D,此时D没有孩纸,返回上一层,创建B的右孩纸E,E没有孩纸节点,继续返回到B,B已经创建完成,返回到A的右孩子C的创建,之后和前面类似。
在这里插入图片描述
遇到‘/’就返回,代表没有左孩子或者没有右孩子

下面附上完整代码和运行截图

#include<stdio.h>
#include<queue>
#include<bits/stdc++.h>//万能头文件,不建议用,太慢
#include<iostream>
using namespace std;

struct tree{
	char data;
	struct tree *lchild,*rchild;
};

int depth(tree*);//求二叉树的深度
void create(tree* &T);//创建树
int numNode(tree *T);//寻找有多少个节点
queue<tree>q;//层序遍历需要用的队列

void create(tree* &T)//创建二叉树 
{
	char c;
	scanf("%c",&c);
	if(c=='/') return;
	T=new tree;
	T->data=c;
	T->lchild=NULL;
	T->rchild=NULL;
	create(T->lchild);
	create(T->rchild); 
}

void priorOrder(tree* T)//先序遍历二叉树
{
	if(T)
	{
		cout<<T->data<<endl;
		priorOrder(T->lchild);
		priorOrder(T->rchild);
	}
} 

void behindOrder(tree* T)//后序遍历二叉树 
{
	if(T)
	{
		behindOrder(T->lchild);
		behindOrder(T->rchild);
		cout<<T->data<<endl;
	}
}

void midOrder(tree *T)//中序遍历二叉树 
{
	if(T)
	{
		midOrder(T->lchild);
		cout<<T->data<<endl;
		midOrder(T->rchild);
	}
}

void cengOrder(tree *T) //层序遍历二叉树 
{
	if(T)
	{
		q.push(*T);
	}
	while(!q.empty())
	{
		cout<<q.front().data;
		if (q.front().lchild != NULL)   //如果有左孩子,lchild入队列
        {
            q.push(*q.front().lchild);   
        }

        if (q.front().rchild != NULL)   //如果有右孩子,rchild入队列
        {
            q.push(*q.front().rchild);
        }
        q.pop();
        if(!q.empty()){
        	cout << " → ";
		}
	}
}

int numNode(tree* T)//计算节点个数
{
	int count=0;
	if(T)
	{
		++count;
		count+=numNode(T->lchild);
		count+=numNode(T->rchild);
	}
	return count;
} 

int depth(tree* T)//计算树的深度
{
	int leftLen,rightLen;
	if(T==NULL)return 0;
	else {
		leftLen=depth(T->lchild)+1;
		rightLen=depth(T->rchild)+1;
	}
	if(leftLen>rightLen)return leftLen;
	else return rightLen;
} 

int numLeaf(tree *T)//计算叶子节点的个数
{
	int num=0;
	if(T)
	{
		if(T->lchild==NULL&&T->rchild==NULL){
			++num;
		}
		num+=numLeaf(T->lchild);
		num+=numLeaf(T->rchild);
	}
    return num;
} 

int main()
{
     struct tree *T=new tree;
     T=NULL;
     cout<<"输入你要创建的二叉树:"<<endl;
     create(T);
     cout<<"节点的个数是:"<<numNode(T)<<endl;
     cout<<"树的节点深度:"<<depth(T)<<endl;
     cout<<"树的叶子节点数:"<<numLeaf(T)<<endl; 
     cout<<"先序遍历:"<<endl;   priorOrder(T);
	 cout<<endl;
	 cout<<"后续遍历:"<<endl;   behindOrder(T);
	 cout<<endl;
	 cout<<"中序遍历:"<<endl;   midOrder(T);
	 cout<<endl;
	 cout<<"层序遍历:"<<endl;   cengOrder(T);
	 cout<<endl; 
	 
	 return 0; 
}


在这里插入图片描述
这里还多一句嘴,其实所有的树都可以转化成二叉树来建立,也就是树的简单形态都可以用递归来实现。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值