数据结构与算法 第06部分:二叉树

1:定义二叉树存储结构

typedef struct Bnode	/*定义二叉树存储结构*/
{
	char data;
	struct Bnode *lchild,*rchild;
}Bnode,*Btree;

2:按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T

//按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
void Createtree(Btree &T,char ch = '#')	/*创建二叉树函数*/
{
	if ('#' == ch)
	{
		T = NULL;			//递归结束,建空树
		return;
	}    
	T = new Bnode;
	T->data = ch;			//生成根结点
	Createtree(T->lchild);	//递归创建左子树
	Createtree(T->rchild);	//递归创建右子树
}

3:先序遍历二叉树

void preorder(Btree T)//先序遍历二叉树
{
    if(nullptr == T)
    {
		return;
    }
	cout << T->data << "  ";
	preorder(T->lchild);
	preorder(T->rchild);
}

4:中序遍历二叉树

void inorder(Btree T)//中序遍历二叉树
{
	if (nullptr == T)
	{
		return;
	}
	inorder(T->lchild);
	cout << T->data << "  ";
	inorder(T->rchild);
}

5:后序遍历二叉树

void posorder(Btree T)//后序遍历二叉树
{
	if (nullptr == T)
	{
		return;
	}
	posorder(T->lchild);
	posorder(T->rchild);
	cout << T->data << "  ";
}

6:层次遍历二叉树(宽度优先)

bool Leveltraverse(Btree T)
{
    if(nullptr == T)
    {
        return false;
    }
    Btree p;
    queue<Btree>Q; //创建一个普通队列(先进先出),里面存放指针类型
    Q.push(T); //根指针入队
    while(!Q.empty()) //如果队列不空
    {
        p = Q.front();//取出队头元素作为当前扩展结点livenode
        Q.pop(); //队头元素出队
        cout<<p->data<<"  ";
        if (nullptr != p->lchild)
        {
            Q.push(p->lchild); //左孩子指针入队
        }        
        if (nullptr != p->rchild)
        {
            Q.push(p->rchild); //右孩子指针入队
        }           
    }
    return true;
}

7:求二叉树的叶子数

int LeafCount(Btree T)//求二叉树的叶子数
{
	if (nullptr == T)//如果为空树,深度为0
	{
		return 0;
	}

	if (nullptr == T->lchild && nullptr == T->rchild)//左右子树均为空,则叶子数为1
	{
		return 1;
	}
	return LeafCount(T->lchild) + LeafCount(T->rchild);//递归计算左子树和右子树的叶子数之和          
}

8:求二叉树的结点数

int NodeCount(Btree T)//求二叉树的结点数
{
	if (nullptr == T)//如果为空树,深度为0
	{
		return 0;
	}
	return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;//递归计算左子树和右子树的结点数之和加1     
}

9:求二叉树的深度

int Depth(Btree T)//求二叉树的深度
{
    int nDepLeft, nDepRight;
	if (nullptr == T)//如果为空树,深度为0
	{
		return 0;
	}
	nDepLeft = Depth(T->lchild);//递归计算左子树深度
	nDepRight = Depth(T->rchild);//递归计算左子树深度
	return (nDepLeft > n) ? nDepLeft + 1 : nDepRight + 1;//返回左右子树最大值加1
}

10:前序中序还原建立二叉树

BiTree pre_mid_createBiTree(char *pre,char *mid,int nLen) //前序中序还原建立二叉树
{
	if (0 == nLen)
	{
		return nullptr;
	}    
    char ch = pre[0];  //找到先序中的第一个结点
    int index = 0;	
    while(mid[index] != ch)//在中序中找到的根结点,根结点的左边为该结点的左子树,右边为右子树
    {
        index++;
    }

    BiTree T = new BiTNode;//创建根结点
    T->data = ch;
    T->lchild = pre_mid_createBiTree(pre+1,mid,index);//建立左子树
    T->rchild = pre_mid_createBiTree(pre+index+1,mid+index+1, nLen -index-1);//建立右子树
    return T;
}
pro_order(T);//输出其后序序列

11:后序中序还原建立二叉树

BiTree pro_mid_createBiTree(char *last,char *mid,int len)//后序中序还原建立二叉树
{
	if (0 == nLen)
	{
		return nullptr;
	}
    char ch = last[len-1]; //取得后序遍历顺序中最后一个结点
    int index =0;//在中序序列中找根结点,并用index记录长度
    while(mid[index] != ch)//在中序中找到根结点,左边为该结点的左子树,右边为右子树
	{
		index++;
	}
    BiTree T = new BiTNode;//创建根结点
    T->data = ch;
    T->lchild = pro_mid_createBiTree(last,mid,index);//建立左子树
    T->rchild = pro_mid_createBiTree(last+index,mid+index+1,len-index-1);//建立右子树
    return T;
}
pre_order(T);//输出其先序序列

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员的资料库

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

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

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

打赏作者

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

抵扣说明:

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

余额充值