C++二叉树结构的建立与基本操作

// Binary_tree.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
#include
#include

using namespace std;

typedef int ElemType;
//typedef char ElemType;

typedef struct TreeNode_s
{
    ElemType Data;
    struct TreeNode_s *pLeft;
    struct TreeNode_s *pRight;
}*Ptree,Btree;

//二叉树的创建
Btree * CreateBiTree(Btree *root, ElemType data)
//void  CreateBiTree(Btree *root, ElemType data)
{

    Btree* pNewNode = NULL;

    pNewNode = new Btree;
    pNewNode->Data = data;
    pNewNode->pLeft = NULL;
    pNewNode->pRight = NULL;
    if (root == NULL)
    {
        root = pNewNode;
        return root;
    }
    else
    {
        Btree* pBackNode = NULL;
        Btree* pCurrentNode = root;
        while(pCurrentNode != NULL)
        {
            pBackNode = pCurrentNode;
            //cout << pCurrentNode->Data<             if(pCurrentNode->Data > data) //查找左子树
            {
                pCurrentNode = pCurrentNode->pLeft;
            }
            else if (pCurrentNode->Data < data) //查找右子树
            {
                pCurrentNode = pCurrentNode->pRight;
            }
            else
            {
                delete pNewNode;
                return pCurrentNode;
            }
        }

        //cout << pBackNode->Data<         //比较插入数据与当前叶子节点,如果大于插入左子树,否则插入右子树
        if (pBackNode->Data > data)
        {
            pBackNode->pLeft = pNewNode;
        }
        else
        {
            pBackNode->pRight = pNewNode;
        }

        return pNewNode;
    }

}

//清空二叉树
void ClearBiTree(Btree* pTreeNode)
{
    if (pTreeNode)
    {
        ClearBiTree(pTreeNode->pLeft);  //清空左子树
        ClearBiTree(pTreeNode->pRight); //清空右子树
        delete pTreeNode;               //释放当前节点所占用的内容
    }
}

//计算二叉树的个数
int BiTreeCount(Btree *root)
{
    if (root == NULL)
    {
        return 0;
    }
    else
    {
        return BiTreeCount(root->pLeft) + BiTreeCount(root->pRight) + 1;
    }
}


//打印二叉树
void PrintBiTree(Btree *root)
{
    Btree *pNode = root;
    if (pNode != NULL)
    {
        cout<Data<< " ";
        PrintBiTree(pNode->pLeft);
        PrintBiTree(pNode->pRight);
    }
}

//查找节点,返回该数据所在节点的指针
Btree* FindTreeNode(Btree *root, ElemType data)
{
    Btree *pNode = root;

    if(pNode == NULL)
    {
        return NULL;
    }
    else
    {
        //查找节点,返回该数据所在节点的指针
        if (pNode->Data == data)
        {
            return pNode;
        }
        else
        {          
            if (data < pNode->Data ) //左子树递归查找
            {
                return FindTreeNode(pNode->pLeft, data);
            }
            else if (data > pNode->Data)  //右子树递归查找
            {
                return  FindTreeNode(pNode->pRight,data);
            }
            else
            {
                return NULL;
            }
        }
    }
}


//查找叶子节点
int FindLeafNodeNum(Btree* root)
{
    static int n = 0;
    Btree *pTreeNode = root;
    if (pTreeNode == NULL)
    {
        return 0;
    }
    else
    {
        if (pTreeNode->pLeft == NULL
            && pTreeNode->pRight == NULL)
        {
            n += 1;
        }
        else
        {
            FindLeafNodeNum(pTreeNode->pLeft);
            FindLeafNodeNum(pTreeNode->pRight);
        }
        return n;
    }

}


//先序遍历二叉树:先访问根,然后遍历左子树,再遍历右子树
void PreOrderBiTree(Btree* root)
{
    Btree* pTreeNode = NULL;
    pTreeNode = root;
    if (pTreeNode ==  NULL)
    {
        return ;
    }
    else
    {
        cout << pTreeNode->Data << " ";
        PreOrderBiTree(pTreeNode->pLeft);
        PreOrderBiTree(pTreeNode->pRight);
    }
}

//中序遍历二叉树:先遍历左子树,然后节点数据域,再遍历右子树
void InOrderBiTree(Btree* root)
{
    Btree* pTreeNode = NULL;
    pTreeNode = root;
    if(pTreeNode == NULL)
    {
        return ;
    }
    else
    {
        InOrderBiTree(pTreeNode->pLeft);
        cout << pTreeNode->Data << " ";
        InOrderBiTree(pTreeNode->pRight);
    }
}

//后序遍历二叉树:先遍历左子树,再遍历右子树,最后节点数据域
void PostOrderBiTree(Btree* root)
{
    Btree* pTreeNode = NULL;
    pTreeNode = root;
    if(pTreeNode == NULL)
    {
        return ;
    }
    else
    {
        PostOrderBiTree(pTreeNode->pLeft);
        PostOrderBiTree(pTreeNode->pRight);
        cout << pTreeNode->Data << " ";
    }
}


//添加节点
void AddTreeNode(Btree *root, ElemType data)
{
    Btree* pParent = NULL;
    Btree* pNode = new Btree;
    pNode->Data = data;
    pNode->pLeft = NULL;
    pNode->pRight= NULL;
    //根节点为空,这创建根节点
    if (root == NULL)
    {
        root = pNode;
    }
    else
    {
        pParent = FindTreeNode(root, data);
        if (!pParent)
        {
           
        }
    }
}

//计算二叉树的深度
int BiTreeDepth(Btree *root)
{
    Btree *pTreeNode = root;
    int depleft = 0;
    int depright = 0;

    if (pTreeNode == NULL)
    {
        return 0;  // 节点为空, 深度为0
    }
    else
    {
        depleft = BiTreeDepth(pTreeNode->pLeft);
        depright = BiTreeDepth(pTreeNode->pRight);
        if (depleft > depright)
        {
            return ++depleft;
        }
        else
        {
            return ++depright;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
     ElemType data;
     Btree *pRoot = NULL;
     Btree* pTreeNode = NULL;
     //构造根节点
     cin>>data;
     pTreeNode = CreateBiTree(pRoot, data);
     pRoot = pTreeNode;

     //添加树节点
    
     //{7,4,2,3,15,36,6,45,55,20,1,14,56,57,58};
     while(1)
     {
         //cout<<"pleasd input node data:"<          cin>>data;
         if (data == '#') //'#' ASSCII = 35
         {
            break;
         }
         pTreeNode = CreateBiTree(pRoot, data);
     }
    
     //先序遍历
     cout<<"PreOrderBiTree"<      PreOrderBiTree(pRoot);
     cout<

     //中序遍历
     cout<<"InOrder Bitnary Tree:"<      InOrderBiTree(pRoot);
     cout<

     //后序遍历
     cout<<"post order Bitnary tree:"<      PostOrderBiTree(pRoot);
     cout<

     //二叉树节点个数
     int num = BiTreeCount( pRoot);
     cout<<"node num:"<

     //二叉树叶子节点个数
     int leafnum = FindLeafNodeNum( pRoot);
     cout<<"leafnode num:"<


    //二叉树深度
    int depth = BiTreeDepth(pRoot);
    cout<<"depth :"< #if 1
     //查找节点 
     cout << "input find data:"<      cin >>data;

     pTreeNode = FindTreeNode(pRoot, data);

     if (pTreeNode != NULL)
     {
         cout<Data<      }
     else
     {
        cout<<"can not find "<      }
#endif

     ClearBiTree(pRoot);


    return 0;

   
}

 

你可能感兴趣的:(C/C++)