数据结构(七)二叉树节点、空指针、删除叶节点、最大节点数

1、二叉树节点

代码:

//二叉树节点 
#include<stdio.h>
#include <malloc.h>
#include <conio.h>
#include<iostream>
//
typedef int DataType; 
typedef struct Node
{
 DataType data;
 struct Node *LChild;
 struct Node *RChild;
}BinaryNode,*BinaryTree;
//用扩展先序遍历序列创建二叉树,如果是#当前树根置为空,否则申请一个新节点
void CreateBinaryTree(BinaryTree *bt)
{
 char ch;
 ch=getchar();
 if(ch=='.')*bt=NULL;
 else
 {
  *bt=(BinaryTree)malloc(sizeof(BinaryNode));
  (*bt)->data=ch;
  CreateBinaryTree(&((*bt)->LChild));
  CreateBinaryTree(&((*bt)->RChild));
 }
}
//访问根节点
void Visit(char ch)
{
 printf("%c  ",ch);
}
//先序遍历二叉树, root为指向二叉树(或某一子树)根结点的指针
void  PreOrder(BinaryTree root) 
{
 if (root!=NULL)
 {
  Visit(root ->data);  //访问根结点
  PreOrder(root ->LChild);  //先序遍历左子树
  PreOrder(root ->RChild);  //先序遍历右子树
 }
}
void  InOrder(BinaryTree root)  
//中序遍历二叉树, root为指向二叉树(或某一子树)根结点的指针
{
 if (root!=NULL)
 {
  InOrder(root ->LChild);   //中序遍历左子树
  Visit(root ->data);        //访问根结点
  InOrder(root ->RChild);   //中序遍历右子树
 }
}
//后序遍历二叉树,root为指向二叉树(或某一子树)根结点的指针
void  PostOrder(BinaryTree root)  
{
 if(root!=NULL)
 {
  PostOrder(root ->LChild); //后序遍历左子树
  PostOrder(root ->RChild); //后序遍历右子树
  Visit(root ->data);       //访问根结点
 }
}
//后序遍历求二叉树的高度递归算法//
int PostTreeDepth(BinaryTree bt)   
{
 int hl,hr,max;
 if(bt!=NULL)
 {
  hl=PostTreeDepth(bt->LChild);  //求左子树的深度 
  hr=PostTreeDepth(bt->RChild);  //求右子树的深度 
  max=hl>hr?hl:hr;              //得到左、右子树深度较大者
  return(max+1);               //返回树的深度
 }
 else return(0);              //如果是空树,则返回0
}
//按竖向树状打印的二叉树 
void PrintTree(BinaryTree Boot,int nLayer)  
{
    int i;
 if(Boot==NULL) return;
 PrintTree(Boot->RChild,nLayer+1);
 for(i=0;i<nLayer;i++)
  printf("  ");
 printf("%c\n",Boot->data);
 PrintTree(Boot->LChild,nLayer+1);
}
//节点 
int nodeCount(Node* T)
{	int nodecount=0;
	while(T!=NULL)
	{
	return 	1+nodeCount(T->LChild)+nodeCount(T->RChild);
	}
	return nodecount;
}
//叶节点 
int leaf(Node* T)
{
	int LeafCount=0;
	if(T==NULL)
	LeafCount=0;
	else
		if(T->LChild==NULL&&T->RChild==NULL)
			LeafCount+=1;
		else
			LeafCount=leaf(T->LChild)+leaf(T->RChild);
	return LeafCount;
}

//满节点
 int fullleaf(Node* &T) 
{if (NULL == T)
		return 0;
	return (T -> LChild != NULL && T -> RChild != NULL) + fullleaf (T -> LChild) + fullleaf (T -> RChild)  ;
}

int main()
{
 BinaryTree T;
 int h;
 int layer;
 int treeleaf;
 layer=0;
 
 printf("请输入二叉树中的元素(以扩展先序遍历序列输入,其中.代表空子树):\n");
    CreateBinaryTree(&T);
 printf("先序遍历序列为:");
 PreOrder(T);
 printf("\n中序遍历序列为:");
 InOrder(T);
 printf("\n后序遍历序列为:");
 PostOrder(T);
 h=PostTreeDepth(T);
    printf("\nThe depth of this tree is:%d\n",h);
 PrintTree(T,layer);
 
 int ch;
 ch=nodeCount(T);
 printf("\n该二叉树的节点数目为:%d\n",ch);
 ch=leaf(T);
 printf("\n该二叉树的叶节点数目为:%d\n",ch);
 ch=fullleaf(T);
 printf("\n该二叉树的满节点数目为:%d\n",ch);


return 0;
}

运行:

2、空指针

代码:

//空指针 
/*设二叉树有n个节点,度为0的n0个,度为1的n1个,度为2的n2个,空链域只有度为1和度为0的有,一共有2n0+n1个空链域。
1.n=n0+n1+n2;
2.n=n1+2n2+1;
1式x2-2式即的结果*/
//二叉树节点 
#include<stdio.h>
#include <malloc.h>
#include <conio.h>
#include<iostream>
typedef int DataType; 
typedef struct BTNode
{
 DataType data;
 struct BTNode *lchild;
 struct BTNode *rchild;
}BTNode,*BinaryTree;
//用扩展先序遍历序列创建二叉树,如果是#当前树根置为空,否则申请一个新节点
void CreateBinaryTree(BinaryTree *bt)
{
 char ch;
 ch=getchar();
 if(ch=='.')*bt=NULL;
 else
 {
  *bt=(BinaryTree)malloc(sizeof(BTNode));
  (*bt)->data=ch;
  CreateBinaryTree(&((*bt)->lchild));
  CreateBinaryTree(&((*bt)->rchild));
 }
}
//求二叉树中度为0的节点个数 
int NumberOfZeroDegree(BTNode *T)
{
    int i=0;
    if(NULL != T)
    {
            if(NULL==T->lchild  && NULL==T->rchild)
            {
                         i=1;       
            }
            else
            {
                i= NumberOfZeroDegree(T->lchild)+NumberOfZeroDegree(T->rchild);
            }
             
    }
    return i;
}

//求二叉树中度为1的节点个数 
int NumberOfOneDegree(BTNode *T)
{
    int i=0;
    if(NULL != T)
    {
           
           if((NULL!=T->lchild  && NULL==T->rchild) ||(NULL!=T->rchild && NULL ==T->lchild))
            {
                 i=1+NumberOfOneDegree(T->lchild)+NumberOfOneDegree(T->rchild);
            }
            else
            {
                 i=NumberOfOneDegree(T->lchild)+NumberOfOneDegree(T->rchild);
            }
            
    }
    return i;
    
}

//求二叉树中度为2的节点个数 
int NumberOfTwoDegree(BTNode *T)

{
    int i=0;
    
    if(NULL != T)
    {
            if((NULL!=T->lchild)&&(NULL!=T->rchild))
              i=1+NumberOfTwoDegree(T->lchild)+NumberOfTwoDegree(T->rchild); 
            else
              i=NumberOfTwoDegree(T->lchild)+NumberOfTwoDegree(T->rchild); 
    }
    return i;
}
//1.n=n0+n1+n2;
int NumberAllNodes1(BTNode *T)
{
    return NumberOfZeroDegree(T)+NumberOfOneDegree(T)+NumberOfTwoDegree(T);
}
//2.n=n1+2n2+1;
int NumberAllNodes2(BTNode *T)
{
    return 2*NumberOfOneDegree(T)+NumberOfTwoDegree(T)+1;
}  

//求 nullptr的值 
int Numberofnullptr(BTNode *T)
{
	return  NumberAllNodes1(T)-1;
}

int main()
{
 BinaryTree T;
 
 printf("请输入二叉树中的元素(以扩展先序遍历序列输入,其中.代表空子树):\n");
    CreateBinaryTree(&T);
  int ch;
 ch=NumberOfZeroDegree(T);
 printf("\n该二叉树度为0的节点数目为:%d\n",ch);
 ch=NumberOfOneDegree(T);
 printf("\n该二叉树度为1的叶节点数目为:%d\n",ch);
 ch=NumberOfTwoDegree(T);
 printf("\n该二叉树度为2的满节点数目为:%d\n",ch);
 ch=NumberAllNodes1(T);
 //ch=NumberAllNodes2(T);
 printf("\n该二叉树的节点数目为:%d\n",ch);
 ch=Numberofnullptr(T);
 printf("\n该二叉树的空指针数目为:%d\n",ch);
 
 
 return 0;
}

运行:

3、删除叶节点

代码:

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
struct BiNode
{
    char data;
    BiNode *lchild, *rchild;
};
class BiTree
{
public:
    BiTree( );
    BiNode * Getroot();
    int Delete(BiNode *root);
    void LeverOrder(BiNode *root);
private:
    BiNode *root;
    BiNode *Creat( );
};
BiTree::BiTree( )
{
    this->root = Creat( );
}
BiNode *BiTree::Getroot( )
{
    return root;
}
int BiTree::Delete(BiNode *root)
{
    if(root==NULL)
        return (int)NULL;
    else
    {
        if(!root->lchild && !root->rchild)//判断是否为叶子
            root->data ='#';//将节点值置空
        Delete(root->lchild);//并删除当前节点的左右孩子
        Delete(root->rchild);
    }
    return 0;
}
void BiTree::LeverOrder(BiNode *root)//层序输出
{
    const int MaxSize = 100;
    int front = 0;
    int rear = 0;
    BiNode* Q[MaxSize];
    BiNode* q;
    if (root == NULL) return;
    else
    {
        Q[rear++] = root;
        while (front != rear)
        {
            q = Q[front++];
            if(q->data!='#')
            cout<<q->data<<" ";
            if (q->lchild != NULL)    Q[rear++] = q->lchild;
            if (q->rchild != NULL)    Q[rear++] = q->rchild;
        }
    }
}
BiNode* BiTree ::Creat( )
{
    BiNode * root;
    char ch;
    cin>>ch;
    if (ch == '#')
        root = NULL;
    else
    {
        root = new BiNode;
        root->data=ch;
        root->lchild = Creat( );
        root->rchild = Creat( );
    }
    return root;
}
//判断是否为叶子,然后删除,最后层序输出就行了
int main()
{
    BiTree bt;
    BiNode *root = bt.Getroot( );
    bt.Delete(root);
    bt.LeverOrder(root);
    return 0;
}

运行:

4、最大节点数

代码:

//最大节点数,则该二叉树为满二叉树 
#include<stdio.h>
#include <conio.h>
#include<iostream>
using namespace std;
 
typedef int DataType; 
typedef struct Node
{
 DataType data;
 struct Node *lChild;
 struct Node *rChild;
}BinTreeNode,*BinaryTree;


//后序遍历求二叉树的高度递归算法
int PostTreeDepth(BinaryTree bt)   
{
 int hl,hr,max;
 if(bt!=NULL)
 {
  hl=PostTreeDepth(bt->lChild);  //求左子树的深度 
  hr=PostTreeDepth(bt->rChild);  //求右子树的深度 
  max=hl>hr?hl:hr;              //得到左、右子树深度较大者
  return(max+1);               //返回树的深度
 }
 else return(0);              //如果是空树,则返回0
}

template<class T>
int BinaryTree<T>::twoDegree(BinTreeNode<T>* bt)
{
    //子树不空
    if(bt!=NULL)
    {
        //如果左右子树都不空
        if(bt->lChild!=NULL 
            && bt->rChild!=NULL)
            //递归统计左右子树,并返回两者的和,并加1
            return 1+twoDegree(bt->lChild)
            +twoDegree(bt->rChild);
		 else
            return 0;
    }
	else
        return 0;
}

int main()
{
  BinaryTree T;
  int h;
  int n;
  
  h=PostTreeDepth(T);
    printf("\nThe depth of this tree is:%d\n",h);
    
  n=BinaryTree(T);
    printf("\nThe jiedian of this tree is:%d\n",n);
    
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/RayMa0305/article/details/81479968