有关于二叉树面试题目:二叉树创建、递归(非递归)、遍历;

二叉树

在这里插入图片描述
本章内容:

#pragma once

typedef char ElemType;
typedef struct BtNode
{
	struct BtNode* leftchild;
	struct BtNode* rightchild;
	ElemType val;
}BtNode,*BinaryTree;

//添加元素
struct BtNode* BuyNode();

//创建二叉树
struct BtNode* CreateTree1();
struct BtNode* CreateTree2(const char *&str);

//递归遍历
void PreOrder(struct BtNode* p);
void InOrder(struct BtNode* p);
void PastOrder(struct BtNode* p);

//非递归方式遍历二叉树
void NicePreOrder(struct BtNode *s);
void NiceInOrder(struct BtNode *s);
void NicePastOrder(struct BtNode *s);

//先序和中序创建树
struct BtNode* CreateTreePI(char *pstr,char *istr,int len);
//中序和后序创建二叉树
struct BtNode* CreateTreeIL(char *istr,char *lstr,int len);

//层次遍历
void LevelOrder1(struct BtNode *p);//ABGCDHEF
void LevelOrder2(struct BtNode *p);//ABGCDHEF

二叉树的结构体:

typedef char ElemType;
typedef struct BtNode
{
	struct BtNode* leftchild;
	struct BtNode* rightchild;
	ElemType val;
}BtNode,*BinaryTree;

添加元素,初始化:

//初始化,添加元素
struct BtNode* BuyNode()
{
	struct BtNode *p=(struct BtNode*)malloc(sizeof(struct BtNode));
	if(NULL==p)
	{
		exit(1);
	}
	memset(p,0,sizeof(struct BtNode));
	return p;
}

创建二叉树:

  • 方法1:输入值字符串,创建二叉树
//创建二叉树
struct BtNode* CreateTree1()
{
	//输入值,创建结点
	ElemType val;
	cin>>val;
	struct BtNode *p=NULL;

	if(val!=END)
	{
		p=BuyNode();
		p->val=val;
		p->leftchild=CreateTree1();
		p->rightchild=CreateTree1();
	}
	return p;
}
  • 方法2:字符串创建二叉树
struct BtNode* CreateTree2(const char *&str)
{
	struct BtNode* p=NULL;
	if(str!=NULL && *str!=END)
	{
		p=BuyNode();
		p->val=*str;
		p->leftchild=CreateTree2(++str);
		p->rightchild=CreateTree2(++str);
	}
	return p;
}

递归遍历二叉树:

  • 先序遍历
void PreOrder(struct BtNode* p)
{
	if(p!=NULL)
	{
		cout<<p->val<<" ";
		PreOrder(p->leftchild);
		PreOrder(p->rightchild);
	}
}
  • 中序遍历
void InOrder(struct BtNode* p)
{
	if(p!=NULL)
	{
		InOrder(p->leftchild);
		cout<<p->val<<" ";
		InOrder(p->rightchild);
	}
}
  • 后序遍历
void PastOrder(struct BtNode* p)
{
	if(p!=NULL)
	{
		PastOrder(p->leftchild);
		PastOrder(p->rightchild);
		cout<<p->val<<" ";
	}
}

##非 递归遍历二叉树:

  • 先序遍历
void NicePreOrder(struct BtNode *s)
{
	if(s==NULL) return;
	stack<struct BtNode*> st;
	st.push(s);
	while(!st.empty())
	{
		s=st.top();
		st.pop();
		cout<<s->val<<" ";
		if(s->rightchild!=NULL)
		{
			st.push(s->rightchild);
		}
		if(s->leftchild!=NULL)
		{
			st.push(s->leftchild);
		}
	}

}
  • 中序遍历
void NiceInOrder(struct BtNode *s)
{
	if(s==NULL) return;
	stack<struct BtNode*> st;

	while(!st.empty() || s!=NULL)
	{
		while(s!=NULL)
		{
			st.push(s);
			s=s->leftchild;
		}
		s=st.top();
		st.pop(); 
		cout<<s->val<<" " ;
		s=s->rightchild;
	}
}
  • 后序遍历
void NicePastOrder(struct BtNode *s)
{
	if(s==NULL) return;
	stack<struct BtNode*> st;

	struct BtNode *tag;

	while(!st.empty() || s!=NULL)
	{
		while(s!=NULL)
		{
			st.push(s);
			s=s->leftchild;
		}
		s=st.top();
		st.pop();
		if(s->rightchild==NULL || s->rightchild==tag)
		{
			cout<<s->val<<" ";
			tag=s;
			s=NULL;
		}
		else
		{
			st.push(s);
			s=s->rightchild;
		}
	}

}

创建二叉树

  • 先序和中序,创建二叉树
//先序和中序创建树
int FindIndex(char *istr,int len,ElemType target)
{
	int i=0;
	for(i;i<len;i++)
	{
		if(istr[i]==target)
		{
			return i;
		}
	}
	return -1;
}
struct BtNode* CreateTreePI(char *pstr,char *istr,int len)
{
	struct BtNode *s=NULL;
	if(len>0)
	{
		//生成结点
		s=BuyNode();
		//将数据放入结点
		s->val=pstr[0];
		//寻找中序结点的位置
		int pos=FindIndex(istr,len,pstr[0]);
		if(pos==-1)
		{
			exit(1);
		}
		//构建左孩子
		return s->leftchild=CreateTreePI(pstr+1,istr,pos);
		//构建右孩子
		return s->rightchild=CreateTreePI(pstr+1+pos,istr+pos+1,len-1-pos);
			
	}
}
  • 中序和后序创建二叉树
struct BtNode* CreateTreeIL(char *istr,char *lstr,int len)
{
	struct BtNode *s=NULL;
	if(len>0)
	{
		//生成结点
		s=BuyNode();
		//将数据放入结点
		s->val=lstr[len-1];
		//寻找中序结点的位置
		int pos=FindIndex(istr,len,lstr[len-1]);
		if(pos<0)
		{
			exit(1);
		}
		//创建左孩子
		return s->leftchild=CreateTreeIL(istr,lstr,pos);
		//创建右孩子
		return s->rightchild=CreateTreeIL(istr+pos+1,lstr+1+pos,len-1-pos);
			
	}
}

层次遍历:

  • ABGCDHEF
void LevelOrder1(struct BtNode *p)//ABGCDHEF
{
	if(p==NULL) return;
	queue<struct BtNode*> que;
	que.push(p);
	while(!que.empty())
	{
		p=que.front();
		que.pop();
		cout<<p->val<<" ";
		if(p->leftchild!=NULL)
		{
			que.push(p->leftchild);
		}
		if(p->rightchild!=NULL)
		{
			que.push(p->rightchild);
		}
	}
	cout<<endl;
}

-AGBCDHFE

void LevelOrder2(struct BtNode *p)//AGBCDHFE
{
	if(p==NULL)	return;
	stack<struct BtNode*>st;
	queue<struct BtNode*>que;

	st.push(p);
	while(!st.empty() || !que.empty())
	{
		while(!st.empty())
		{
			p=st.top();
			st.pop();
			cout<<p->val<<" ";
			if(p->rightchild!=NULL)
			{
				que.push(p->rightchild);
			}
			if(p->leftchild!=NULL)
			{
				que.push(p->leftchild);
			}
		}
		
		while(!que.empty())
		{
			p=que.front();
			que.pop();
			cout<<p->val<<" ";
			if(p->rightchild!=NULL)
			{
				st.push(p->rightchild);
			}
			if(p->leftchild!=NULL)
			{
				st.push(p->leftchild);
			}
		}
	}
	cout<<endl;
}

测试用例:

main函数:

#include <iostream>
#include "Tree.h"

using namespace std;

int main()
{
	BinaryTree root=NULL;
	const char *str="ABC##DE##F##G#H##";

	//root=CreateTree1();
	root=CreateTree2(str);

	NicePreOrder(root);
	cout<<endl;
	NiceInOrder(root);
	cout<<endl;
	NicePastOrder(root);
	cout<<endl;

	char *pstr="ABCDEFGH";//先序
	char *istr="CBEDFAGH";//中序
	char *lstr="CEFDBHGA";//后序
	int len=strlen(pstr);
	CreateTreeIL(pstr,istr,len);
	NicePreOrder(root);
	cout<<endl;
	LevelOrder1(root);
	LevelOrder2(root);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值