二叉树

一、二叉树的逻辑结构
(一)定义:
二叉树是n(n≥0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树的二叉树组成。

(二)二叉树的特点:
⑴ 每个结点最多有两棵子树;
⑵ 二叉树是有序的,其次序不能任意颠倒。
二叉树和树是两种树结构。

(三)特殊的二叉树:
1.斜树
(1)所有结点都只有左子树的二叉树称为左斜树
(2)所有结点都只有右子树的二叉树称为右斜树
在斜树中,每一层只有一个结点;
斜树的结点个数与其深度相同。
2.满二叉树
满二叉树在同样深度的二叉树中结点个数最多
满二叉树在同样深度的二叉树中叶子结点个数最多
3.完全二叉树
对一棵具有n个结点的二叉树按层序编号,如果编号为i(1≤i≤n)的结点与同样深度的满二叉树中编号为i的结点在二叉树中的位置完全相同。
对一棵具有n个结点的完全二叉树中从1开始按层序编号,则
结点i的双亲结点为i/2;结点i的左孩子为2i; 结点i的右孩子为2i+1
特点:
(1)叶子结点只能出现在最下两层,且最下层的叶子结点都集中在二叉树的左部;
(2)完全二叉树中如果有度为1的结点,只可能有一个,且该结点只有左孩子。
(3)深度为k的完全二叉树在k-1层上一定是满二叉树。

(四)二叉树的基本性质
1.二叉树的第i层上最多有2i-1个结点(i≥1)。
2.一棵深度为k的二叉树中,最多有2k-1个结点,最少有k个结点。
3. 在一棵二叉树中,如果叶子结点数为n0,度为2的结点数为n2,则有: n0=n2+1。
4. 具有n个结点的完全二叉树的深度为 log2n(向下取整)+1
5. 对一棵具有n个结点的完全二叉树中从1开始按层序编号,则对于任意的序号为i(1≤i≤n)的结点(简称为结点i),有:
(1)如果i>1,
则结点i的双亲结点的序号为 i/2;如果i=1,
则结点i是根结点,无双亲结点。
(2)如果2i≤n,
则结点i的左孩子的序号为2i;
如果2i>n,则结点i无左孩子。
(3)如果2i+1≤n,
则结点i的右孩子的序号为2i+1;
如果2i+1>n,则结点 i无右孩子。

(五)二叉树的遍历操作
1.前序(根)遍历:
若二叉树为空,则空操作返回;
否则:
①访问根结点;
②前序遍历根结点的左子树;
③前序遍历根结点的右子树。
2.中序(根)遍历
若二叉树为空,则空操作返回;
否则:
①中序遍历根结点的左子树;
②访问根结点;
③中序遍历根结点的右子树。
3.后序(根)遍历
若二叉树为空,则空操作返回;
否则:
①后序遍历根结点的左子树;
②后序遍历根结点的右子树。
③访问根结点;
4.层序遍历
二叉树的层次遍历是指从二叉树的第一层(即根结点)开始,从上至下逐层遍历,在同一层中,则按从左到右的顺序对结点逐个访问。

二、二叉树的存储结构及实现
(一)顺序存储结构
二叉树的顺序存储结构就是用一维数组存储二叉树中的结点,并且结点的存储位置(下标)应能体现结点之间的逻辑关系——父子关系。
前序遍历

void Preorder(int root, char data[]){
	if(data[root]!='\0'){
		cout<<data[root] ;			
		Preorder(2*root,data);
		Preorder(2*root+1,data);
	}
	return;
}

中序遍历

void InOrder(int root, char data[]){
	if(data[root]!='\0'){
		InOrder(2*root,data);
		cout<<data[root] ;			
		 InOrder(2*root+1,data);	
	}
	return;
}

后序遍历

void PostOrder(int root, char data[]){
	if(data[root]!='\0'){
		 PostOrder(2*root,data);
		 PostOrder(2*root+1,data);
		cout<<data[root] ;			
	}
	return;
}

构建:

void create(char preorder[],char inorder[],int start_p, int end_p,int start_i,int end_i, char data[],int root){
	if(start_p>end_p)
		return ;
	else{
		int k;
		for(int i=start_i;i<=end_i;i++){
			if(inorder[i]==preorder[start_p]){
				k=i;
				break;
			}
		}
		data[root]=preorder[start_p];
		create(preorder,inorder,start_p+1,start_p+k-start_i,start_i,k-1,data, 2*root);
		create(preorder,inorder,start_p+k-start_i+1,end_p,k+1,end_i,data,2*root+1);
	}
	return ;
}

(二)二叉链表
1.基本思想:
令二叉树的每个结点对应一个链表结点,链表结点除了存放与二叉树结点有关的数据信息外,还要设置指示左右孩子的指针。
在这里插入图片描述

data:数据域,存放该结点的数据信息;
lchild:左指针域,存放指向左孩子的指针;
rchild:右指针域,存放指向右孩子的指针。

节点信息

template <class T>
struct BiNode
{
    T data;
    BiNode<T> *lchild, *rchild;
};

类声明

template <class T>
class BiTree
{    
  public:
       BiTree(); 
        ~BiTree( );            
        void PreOrder(){PreOrder(root);} 
        void InOrder() {InOrder(root);} 
        void PostOrder() {PostOrder(root);} 
        void LevelOrder(){LeverOrder(root)};
  private:
        BiNode<T> *root; 
        BiNode<T> * Creat( ); 
        void Release(BiNode<T> *root);
        void PreOrder(BiNode<T> *root); 
        void InOrder(BiNode<T> *root); 
        void PostOrder(BiNode<T> *root); 
        void LevelOrder(BiNode<T> *root);
 };

前序遍历——递归算法

template   <class T>
void   BiTree::PreOrder(BiNode<T> *root) 
{
        if (root ==NULL)  return;     
        else {
            cout<<root->data;         
            PreOrder(root->lchild);    
            PreOrder(root->rchild);    
        }
 }

前序遍历——非递归算法

template <class T>
void BiTree::PreOrder(BiNode<T> *root) {
  SeqStack<BiNode<T> *>  s;
     while (root!=NULL | | !s.empty()) {
         while (root!= NULL)  {
             cout<<root->data;
             s.push(root);
             root=root->lchild;  
         }
         if (!s.empty()) { 
             root=s.pop();
             root=root->rchild;  
         }
     }
}

扩展前序遍历序列(叶子节点的左右指针域都指向"#")

///带返回值,不传参
template <class T>
BiTree ::BiTree(){ 
      root=creat()}

template <class T>
BiNode<T> * BiTree ::Creat(){
     BiNode<T> *root; char ch;
    cin>>ch;
    if (ch=='#')     root=NULL; 
    else {
        root=new BiNode<T>; 
        root->data=ch;
        root->lchild=creat(); 
        root->rchild= creat(); 
    }  
  return root
}

template <class T>
BiNode<T> * BiTree ::Creat(){
     BiNode<T> *root; char ch;
    cin>>ch;
    if (ch=='# ')     root=NULL; 
    else {
        root=new BiNode<T>; 
        root->data=ch;
        root->lchild=creat(); 
        root->rchild= creat(); 
    }  
  return root
}
///传参,不带返回值
template <class T>
void BiTree<T>::Creat(BiNode<T> * &root  )
{
	    T ch;
	    cout<<"请输入创建一棵二叉树的结点数据"<<endl;
	    cin>>ch;
         if (ch=="#") root = NULL;
         else{ 
	           root = new BiNode<T>;       //生成一个结点
                root->data=ch;
               Creat(root->lchild );    //递归建立左子树
               Creat(root->rchild);    //递归建立右子树
    } 
}

可以设置一个标志量,这样就可以模板化了

中序遍历——递归算法

template <class T>
void BiTree::InOrder (BiNode<T> *root)
{
         if (root==NULL) return;     
         else {
               InOrder(root->lchild); 
               cout<<root->data; 
               InOrder(root->rchild);
         }
}
template<class T>
BiTree<T>::BiTree( )
{
	Creat(root);
}
template <class T>
void BiTree<T>::Creat(BiNode<T> * &root  )
{
	T ch;
	cout<<"请输入创建一棵二叉树的结点数据"<<endl;
	cin>>ch;
         if (ch=="#") root = NULL;
         else{ 
	     root = new BiNode<T>; 
                  root->data=ch;
                  Creat(root->lchild );
                  Creat(root->rchild); 
    } 
}

非递归中序遍历二叉树

template <class T>
void BiTree::InOrderwithoutD (BiNode<T> *root)
{
    stack< BiNode<T> * > aStack;
    while(root)
    {
        aStack.push(root);
        root=root->lchild;
    }
    if(!aStack.empty())
    {
        root=aStack.top();
        aStack.pop();
        cout<<root->data;
        root=root->rchild;
    }
}

非递归后序遍历二叉树

enum Tags {Left,Right};	//特征标识定义
template <class T>
class StackElement		//栈元素的定义
{
public:
    BiTreeNode<T>* pointer;     //指向二叉树结点的指针
    Tags tag; //特征标识申明
};
template<class T>
void BiTree<T>::PostOrderWithoutRecusion(BiTreeNode<T>* root)
{
    StackElement<T> element;
    stack<StackElement<T > > aStack;//栈申明
    BiTreeNode<T>* pointer;
    if(root==NULL)
        return;//空树即返回
    else
        pointer=root;
    while(true)
    {
        while(pointer!=NULL) //进入左子树
        {
            element.pointer=pointer;
            element.tag=Left; //沿左子树方向向下周游
            aStack.push(element);
            pointer=pointer->lchild;
        }
        element=aStack.pop();
        pointer=element.pointer;
        while(element.tag==Right)
        {
            cout<<pointer->data;
            if(aStack.empty())
                return;
            else
            {
                element=aStack.pop();
                pointer=element.pointer;
            }//end else
        } //endwhile
        element.tag=Right;
        aStack.push(element);
        pointer=pointer->rchild();
    }//end while
}

非递归的后续遍历算法2

void tree::T_print(bnode *bt){
    stack<bnode*> s;
    bnode *cur, *pre=NULL;
    if (root==NULL) return;
    s.push(bt);
    while (!s.empty())    {
        cur=s.top();
        if ((cur->Lchild==NULL&&cur->Rchild==NULL)    ||(pre!=NULL&&(pre==cur->Lchild||pre==cur->Rchild)))
        {
            cout<<cur->data;            s.pop();            pre=cur;
        }
        else
        {
            if (cur->Rchild!=NULL)            s.push(cur->Rchild);
            if (cur->Lchild!=NULL)            s.push(cur->Lchild);
        }
    }
}

二叉树 层次遍历

template<class T>
void BiTree<T>::LevelOrder(BinaryTreeNode<T>* root)
{
    queue<BiTreeNode<T>*> aQueue;
    if(root)
        aQueue.push(root);
    while(!aQueue.empty())
    {
        root=aQueue.front(); //取队列首结点
        aQueue.pop();
        cout<<pointer->data;//访问当前结点
        if(root->lchild)	//左子树进队列
            aQueue.push(root->lchild);
        if(root->rchild) //右子树进队列
            aQueue.push(root->rchild);
    }//end while
}

二叉树 的析构

template<class T>
void BiTree<T>::Release(BiNode<T>* root){
  if (root != NULL){                  
      Release(root->lchild);   //释放左子树
      Release(root->rchild);   //释放右子树
      delete root;
  }  
}

求二叉树的结点个数

template<class T>
BiTree<T>::~BiTree(void)
{
	Release(root);
}
void Count(BiNode *root){
    if (root) {
         Count(root->lchild);
         number+ +;  //number为数据成员
         Count(root->rchild);
   }
}
template<class T>
int BiTree<T>::count(BiNode<T>* root){
	int number=0;
	if (root==NULL)
		number=0;
	else
		number=count(root->lchild)+count(root->rchild)+1;
	return number;
}

树中叶子节点的数目

template<class T>
int BiTree<T>::leafcount(BiNode<T>* root){
	int number=0;
	if (root==NULL)
		number=0;
	else if(root->lchild==NULL && root->rchild==NULL)
		number=1;
	else
	    number=leafcount(root->lchild)+leafcount(root->rchild);
	return number;
}

计算树的高度

template<typename T> 
 int BiTree<T>::cal_height(BiTreeNode<T> * root){
	int lheight=0,rheight=0;
	if (root==0)  	 return 0;	
	lheight=cal_height(root->lchild);
	rheight=cal_height(root->rchild);
	if (lheight>rheight)	return lheight+1;
	else 		return rheight+1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值