zoukankan      html  css  js  c++  java
  • 《数据结构与算法(C语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法

    PS:所有的代码示例使用的都是这个图


     

    2019-10-29

     利用p126的算法5.3建立二叉树,并完成三种遍历算法 中序 后序 先序

    #include<iostream>
    #include<stack>
    #define TElemType char
    using namespace std;
    typedef struct BiTNode
    {
    	TElemType data;
    	struct BiTNode *lchild,*rchild;
    	
    }BiTNode,*BiTree;
    //先序遍历的顺序建立二叉链表
    void xCreateBiTree(BiTree &T)
    {
    	char ch;
    	cin>>ch;
    	if(ch=='#') T=NULL;
    	else
    	{
    		T=new BiTNode;
    		T->data=ch;
    		xCreateBiTree(T->lchild);
    		xCreateBiTree(T->rchild);
    	}
    } 
    //先序遍历输出 
    void xPrintTree(BiTree T) 
    {
    
    	if(T)
    	{
    		cout<<T->data;
    		xPrintTree(T->lchild);
    		xPrintTree(T->rchild);
    		
    	}
    	else
    	{
    	 	 return;	 
    	}
    }
    //中序遍历的顺序建立二叉链表
    void zCreateBiTree(BiTree &T)
    {
    	char ch;
    	cin>>ch;
    	if(ch=='#') T=NULL;
    	else
    	{
    		T=new BiTNode;
    		zCreateBiTree(T->lchild);
    		T->data=ch;
    		zCreateBiTree(T->rchild);
    	}
    }
    //中序遍历输出 
    void zPrintTree(BiTree T) 
    {
    
    	if(T)
    	{
    		zPrintTree(T->lchild);
    		cout<<T->data;
    		zPrintTree(T->rchild);
    	}
    	else
    	{
    	 	 return;	 
    	}
    }
    //后序遍历的顺序建立二叉链表
    void hCreateBiTree(BiTree &T)
    {
    	char ch;
    	cin>>ch;
    	if(ch=='#') T=NULL;
    	else
    	{
    		T=new BiTNode;
    		hCreateBiTree(T->lchild);
    		hCreateBiTree(T->rchild);
    		T->data=ch;
    	}
    }
    //后序遍历输出 
    void hPrintTree(BiTree T) 
    {
    
    	if(T)
    	{
    		hPrintTree(T->lchild);
    		hPrintTree(T->rchild);
    		cout<<T->data;
    	}
    	else
    	{
    	 	 return;	 
    	}
    }
    int main()
    {
    	BiTree root;
    //	xCreateBiTree(root);
    //	xPrintTree(root);
    	/*
    		输入:ABC##DE#G##F###
    		输出:ABCDEGF
    	*/
    	
    //	zCreateBiTree(root);
    //	zPrintTree(root);
    	/*
    		输入:ABC##DE#G##F###
    		输出:CBEGDFA
    	*/
    	hCreateBiTree(root);
    	hPrintTree(root);
    	/*
    		输入:ABC##DE#G##F###
    		输出:CGEFDBA
    	*/
    	
    	return 0;
     } 

     


    2019-11-04 

    利用p126的算法5.3建立二叉树,并完成四种遍历算法 中序 先序 后序 层次

    输入:ABC##DE#G##F###
    #include<iostream>
    #include<stack>
    #include<queue>
    #define TElemType char
    #define status int
    #define OK 1
    #define OVERFLOW 0
    using namespace std;
    typedef struct BiTNode
    {
        TElemType data;
        struct BiTNode *lchild,*rchild;
    }BiTNode,*BiTree;
    //先序遍历输出
    void xPrintTree(BiTree T)
    {
      
        if(T)
        {
            cout<<T->data;
            xPrintTree(T->lchild);
            xPrintTree(T->rchild);
              
        }
        else
        {
             return;   
        }
    }
    //中序遍历输出
    void zPrintTree(BiTree T)
    {
      
        if(T)
        {
            zPrintTree(T->lchild);
            cout<<T->data;
            zPrintTree(T->rchild);
        }
        else
        {
             return;   
        }
    }
    //后序遍历输出
    void hPrintTree(BiTree T)
    {
      
        if(T)
        {
            hPrintTree(T->lchild);
            hPrintTree(T->rchild);
            cout<<T->data;
        }
        else
        {
             return;   
        }
    }
    //层次遍历输出 
    void LPrintTree(BiTree T)
    {
        queue<BiTree>q;
        q.push(T);
        while(!q.empty())
        {
            BiTree head=q.front();
            q.pop();
            cout<<head->data;
            if(head->lchild)
            {
                q.push(head->lchild);
            }
            if(head->rchild)
            {
                q.push(head->rchild);
            }
        }
    } 
    void CreateBiTree(BiTree &T)
    {
        TElemType ch;
        cin>>ch;
        if(ch=='#')
        {
            T=NULL;
        }
        else
        {
            T=new BiTNode;
            T->data = ch;
            CreateBiTree(T->lchild);
            CreateBiTree(T->rchild);
        }
    }
    int main()
    {
        BiTree root;
        CreateBiTree(root);
        cout<<"
    先序遍历输出"<<endl;
          xPrintTree(root);
          cout<<"
    中序遍历输出"<<endl;
          zPrintTree(root);
          cout<<"
    后序遍历输出"<<endl;
        hPrintTree(root);
        cout<<"
    层次遍历输出"<<endl;
        LPrintTree(root);
        return 0;
    }  

    设计四个算法,分别实现:
    (1)统计二叉树中结点的个数。
    (2)统计二叉树中叶子结点的个数
    (3)统计二叉树中度为2的结点的个数
    (4)统计二叉树中度为1的结点的个数

    /** 
    	#图P121(b) 
     	#建立二叉树,并且用三种遍历法遍历他 
     	#2019/11/4 
     	输入:
    	 	ABC##DE#G##F###
    	输出:
    		这棵树的节点数:7
    		这棵树的叶子节点数:3
    		这棵树的度为2的结点的数:2
    		这棵树的度为1的结点的数:2
    */
    #include<iostream>
    #include<stack>
    #define TElemType char
    #define status int 
    #define OK 1
    #define OVERFLOW 0
    using namespace std;
    typedef struct BiTNode
    {
        TElemType data;
        struct BiTNode *lchild,*rchild;
         
    }BiTNode,*BiTree;
    //统计二叉树中结点的个数
    int CountTreeNode(BiTree T,int &count)
    {
     
        if(T)
        {
            count++;
            CountTreeNode(T->lchild,count);
            CountTreeNode(T->rchild,count);
             
        }
        else
        {
             return count;    
        }
    }
    //统计二叉树中叶子结点的个数
    int CountTreeLeaf(BiTree T,int &count)
    {
     
        if(T)
        {
        	if(!T->lchild&&!T->rchild)
            	count++;
            CountTreeLeaf(T->lchild,count);
            CountTreeLeaf(T->rchild,count);
        }
        else
        {
             return count;    
        }
    }
    //统计二叉树中度为2的结点的个数
    int CountTreeDegreeTwo(BiTree T,int &count)
    {
     
        if(T)
        {
        	if(T->lchild&&T->rchild)
            	count++;
            CountTreeDegreeTwo(T->lchild,count);
            CountTreeDegreeTwo(T->rchild,count);
        }
        else
        {
             return count;    
        }
    }
    //统计二叉树中度为1的结点的个数
    int CountTreeDegreeOne(BiTree T,int &count)
    {
     
        if(T)
        {
        	if((T->lchild&&!T->rchild)||(!T->lchild&&T->rchild))
            	count++;
            CountTreeDegreeOne(T->lchild,count);
            CountTreeDegreeOne(T->rchild,count);
        }
        else
        {
             return count;    
        }
    }
    void CreateBiTree(BiTree &T)
    {
    	TElemType ch;
    	cin>>ch;
    	if(ch=='#') 
    	{
    		T=NULL;	
    	}
    	else
    	{
    		T=new BiTNode;
    		T->data = ch;
    		CreateBiTree(T->lchild);
    		CreateBiTree(T->rchild);
    	}
    }
    int main()
    {
        BiTree root;
        CreateBiTree(root);
    	int count=0;
        cout<<"这棵树的节点数:"<<CountTreeNode(root,count)<<endl; 
        count=0;
        cout<<"这棵树的叶子节点数:"<<CountTreeLeaf(root,count)<<endl; 
        count=0;
        cout<<"这棵树的度为2的结点的数:"<<CountTreeDegreeTwo(root,count)<<endl;
    	count=0;
        cout<<"这棵树的度为1的结点的数:"<<CountTreeDegreeOne(root,count)<<endl; 
        
        return 0;
    }
  • 相关阅读:
    台州 OJ 3847 Mowing the Lawn 线性DP 单调队列
    洛谷 OJ P1417 烹调方案 01背包
    快速幂取模
    台州 OJ 2649 More is better 并查集
    UVa 1640
    UVa 11971
    UVa 10900
    UVa 11346
    UVa 10288
    UVa 1639
  • 原文地址:https://www.cnblogs.com/chrysanthemum/p/11762574.html
Copyright © 2011-2022 走看看