二叉树的链式存储

实现二叉树的基本操作:建立、遍历、计算深度、结点数、叶子数等。
输入C,先序创建二叉树,#表示空节点;
输入H:计算二叉树的高度;
输入L:计算二叉树的叶子个数;
输入N:计算二叉树节点总个数;
输入1:先序遍历二叉树;
输入2:中序遍历二叉树;
输入3:后续遍历二叉树;
输入F:查找值=x的节点的个数;

输入P:以缩格文本形式输出所有节点。

测试输入:

C
ABC##DE#G##F###
H
L
N
1
2
3
F
A

P

期待的输出:

Created success!
Height=5.
Leaf=3.
Nodes=7.
Preorder is:A B C D E G F .
Inorder is:C B E G D F A .
Postorder is:C G E F D B A .
The count of A is 1.
The tree is:
A
  B
    C
    D
      E
        G
      F

#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
int aa=0;
class BinaryTree  ;
class BinTreeNode   //定义结点类
{
    friend class BinaryTree;
public:
    BinTreeNode ( )
    {
        leftChild =NULL;
        rightChild =NULL;
    }
    BinTreeNode(char x,BinTreeNode *left=NULL,BinTreeNode *right=NULL ):data(x),leftChild(left),rightChild(right) //gouzao
    { 
    }               
    ~BinTreeNode ( ) { } //析构函数
private:
    BinTreeNode *leftChild,*rightChild;   //左、右子女链域
    char data;   //数据域
};
class BinaryTree
{
public:
    BinaryTree():root(NULL) //gen
    {
    }
    BinaryTree(char value)
    {
        RefValue =value;
        root =NULL;
    }
    ~BinaryTree()
    {
        destroy (root);
    }
    void CreateBinTree( )//创建二叉树
    {
        CreateBinTree(root);
        cout<<"Created success!"<<endl;
    }
    bool IsEmpty ()
    {
        return (root == NULL) ? true : false;
    }
    BinTreeNode  *Parent ( BinTreeNode  *current )
    {
        return (root == NULL || root == current)?NULL : Parent ( root, current );
    }
    BinTreeNode  *LeftChild (BinTreeNode  *current )
    {
        return (current != NULL )? current->leftChild :NULL;
    }
    BinTreeNode  *RightChild (BinTreeNode  *current )
    {
        return ( current!= NULL) ? current->rightChild : NULL;
    }
    int Height( )
    {
        return Height(root);
    }
    int Size( )
    {
        return Size(root);
    }
    int Leaf()
    {
        return Leaf(root);
    }
    BinTreeNode  *GetRoot ( ) const
    {
        return root;
    }
    void preOrder( )
    {
        cout<<"Preorder is:";    //前序遍历
        preOrder(root);
        cout<<"."<<endl;
    }
    void inOrder( )
    {
        cout<<"Inorder is:";    //中序遍历
        inOrder(root);
        cout<<"."<<endl;
    }
    void postOrder( )
    {
        cout<<"Postorder is:";   //后序遍历
        postOrder(root);
        cout<<"."<<endl;
    }
    void levelOrder( ) ;
    void pre(char A1)
    {
        pre(root,A1);
        cout<<aa;
    }
    int output()
    {
        return output(root,0);
    }
private:
    BinTreeNode  *root;                //二叉树的根指针
    char RefValue;                    //数据输入停止标志
    void CreateBinTree( BinTreeNode * & subTree);
    BinTreeNode  *Parent ( BinTreeNode * subTree, BinTreeNode  *current );
    int Height(BinTreeNode  *subTree);
    int Size(BinTreeNode  *subTree);
    int Leaf(BinTreeNode  *subTree);
    void preOrder(BinTreeNode * subTree );     //前序遍历
    void inOrder(BinTreeNode * subTree );       //中序遍历
    void postOrder(BinTreeNode * subTree );     //后序遍历
    void pre(BinTreeNode * subTree,char A1);
    int output(BinTreeNode * subTree,int n);
    void destroy(BinTreeNode * &subTree);
};
void BinaryTree::destroy (BinTreeNode* &subTree)
{
                                                          //私有函数: 删除根为subTree的子树
    if (subTree != NULL)
    {
        destroy (subTree->leftChild);     //删除左子树
        destroy (subTree->rightChild);   //删除右子树
        delete subTree; 			     //删除根结点
    }
};

void BinaryTree ::CreateBinTree(BinTreeNode* & subTree)  //私有函数: 建立根为subTree的子树
{
    char item;
    cin>>item;
    if (item != RefValue)
    {
        subTree = new   BinTreeNode (item);
        CreateBinTree( subTree->leftChild);
        CreateBinTree( subTree->rightChild);
    }
    else
        subTree = NULL; 
}

int BinaryTree ::Height(BinTreeNode * subTree )//数的高度
{
    if (subTree ==NULL)
        return 0;  //空二叉树的深度为0
    else
        return 1+max(Height(subTree->leftChild), Height(subTree->rightChild ) );
}
int BinaryTree ::Size( BinTreeNode  * subTree )    //私有函数,计算根指针为subTree的二叉树中的结点个数
{
    if (subTree == NULL )
        return 0;
    return 1+Size(subTree->leftChild)+Size(subTree->rightChild);
}

int BinaryTree::Leaf(BinTreeNode *subTree)    //私有函数,计算根指针为subTree的二叉树中的叶子个数
{
    if(subTree==NULL)
        return 0;
    else if(subTree->leftChild==NULL&&subTree->rightChild==NULL)
        return 1;
    else
        return (Leaf(subTree->leftChild)+Leaf(subTree->rightChild));

}
void BinaryTree ::inOrder ( BinTreeNode  *subTree )
{                                                           //私有函数,中序遍历以subTree为根的二叉树
    if (subTree != NULL)
    {
        inOrder (subTree->leftChild);
        cout<<subTree->data<<" ";
        inOrder (subTree->rightChild );
    }
}
void BinaryTree ::preOrder( BinTreeNode  *subTree )
{
                                                             //私有函数,前序遍历以subTree为根的二叉树
    if (subTree != NULL )
    {
        cout << subTree->data<<" ";
        preOrder (subTree->leftChild );
        preOrder (subTree->rightChild );
    }
}
void BinaryTree ::pre( BinTreeNode *subTree,char A1)      //遍历寻找有几个等于A1
{
    if (subTree != NULL )
    {
    if(subTree->data==A1)
        aa++;
        pre (subTree->leftChild,A1);
        pre (subTree->rightChild,A1);
    }
}
void BinaryTree ::postOrder( BinTreeNode *subTree )
{
                                                              //私有函数,后序遍历以subTree为根的二叉树
    if (subTree != NULL )
    {
        postOrder (subTree->leftChild );
        postOrder (subTree->rightChild );
        cout << subTree->data<<" ";
    }
}
int BinaryTree::output(BinTreeNode *subTree,int n)
{
    if(subTree==NULL)
        return 0;
    for(int i=0; i<n; i++)
    {
        cout<<"  ";
    }
    cout<<subTree->data<<endl;
    if(subTree->leftChild!=NULL||subTree->rightChild!=NULL)
    {
        output(subTree->leftChild,n+1);
        output(subTree->rightChild,n+1);
    }

}
int main()
{
    BinaryTree a('#');
    char w;
    for(int i=0; i<20; i++)
    {
        cin>>w;
        switch(w)
        {
        case'C': a.CreateBinTree();break;
        case'H':
                 cout<<"Height="<<a.Height()<<"."<<endl;
                 break;
        case'L':
                 cout<<"Leaf="<<a.Leaf()<<"."<<endl;
                 break;
        case'N':
                 cout<<"Nodes="<<a.Size()<<"."<<endl;
                 break;
        case'1': a.preOrder();break;
        case'2': a.inOrder();break;
        case'3': a.postOrder();break;
        case'F': char A1;
                 cin>>A1;
                 cout<<"The count of "<<A1<<" is ";
                 a.pre(A1);
                 cout<<"."<<endl;break;
        case'P':
                 cout<<"The tree is:"<<endl;
                 a.output();
                 exit(0);
        }
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_38717135/article/details/79996482