C++ Exercises(十六)--二叉树的简单实现

简介:
#include "stdafx.h"
#include <iostream>
#include <stack>
#include "BinSTree.h"
#include <queue>
using namespace std;

class CTreeNode
{//树节点类
public:
    CTreeNode(const int& item,CTreeNode* lptr = NULL,CTreeNode* rptr = NULL):data(item),left(lptr),right(rptr)
    {
    }
    CTreeNode* Left(void)
    {
        return left;
    }
    CTreeNode* Right(void)
    {
        return right;
    }
    friend class CBinSTree;
public:
    int data;
private:
    CTreeNode* left;
    CTreeNode* right;

};

typedef enum{LEFT,RIGHT} TagType;//伪节点类型
struct ProTreeNode
{//用于后序遍历的伪节点
    CTreeNode* node;
    TagType type;
};
CTreeNode* GetTreeNode(const int& item,CTreeNode* lptr=NULL,CTreeNode* rptr=NULL)
{
    CTreeNode* p;
    p = new CTreeNode(item,lptr,rptr);
    if(p==NULL)
    {
        cerr<<"分配内存失败!"<<endl;
        exit(1);
    }
    return p;
}

void FreeTreeNode(CTreeNode* p)
{
    delete p;
    p = NULL;
}

void InOrder(CTreeNode* t)
{//中序遍历
    stack<CTreeNode*> s;
    CTreeNode* p=t;
    while (p!=NULL || !s.empty())
    {
        while (p!=NULL)             //遍历左子树
        {
            s.push(p);
            p=p->Left();
        }//endwhile
        
        if (!s.empty())
        {
            p=s.top();
            s.pop();
            cout<<p->data<<endl;        //访问根结点
            p=p->Right();            //通过下一次循环实现右子树遍历
        }//endif   
   
    }//endwhile
}

void PreOrder(CTreeNode* t)
{//前序遍历
    stack<CTreeNode*> s;
    CTreeNode* p=t;
    while (p!=NULL || !s.empty())
    {
        while (p!=NULL)             //遍历左子树
        {
            cout<<p->data<<endl;
            s.push(p);
            p=p->Left();
        }//endwhile
        
        if (!s.empty())
        {
            p=s.top();
            s.pop();
            p=p->Right();            //通过下一次循环实现右子树遍历
        }//endif   
   
    }//endwhile
}

void PostOrder(CTreeNode* t)
{//后序遍历
    stack<ProTreeNode> s;
    CTreeNode* p=t;
    ProTreeNode x;
    do
    {
        while (p!=NULL)        //遍历左子树
        {
            x.node = p;
            x.type = LEFT;         //标记为左子树
            s.push(x);
            p=p->Left();
        }
   
        while (!s.empty() && s.top().type==RIGHT)  
        {
            x = s.top();
            s.pop();
            p = x.node;
            cout<<p->data<<endl;   //type为RIGHT,表示右子树访问完毕,故访问根结点    
        }
        
        if (!s.empty())
        {
            s.top().type = RIGHT;     //遍历右子树
            p=s.top().node->Right();        
        }   
    }while (!s.empty());
}
CTreeNode* MakeSampleTree()
{
    CTreeNode *root,*node2,*node3,*node4,*node5;
    node4 = GetTreeNode(4);
    node2 = GetTreeNode(2,NULL,node4);
    node5 = GetTreeNode(5);
    node3 = GetTreeNode(3,node5);
    root = GetTreeNode(1,node2,node3);
    return root;
}

void DeleteTree(CTreeNode* t)
{
    if(t!=NULL)
    {
        DeleteTree(t->Left());
        DeleteTree(t->Right());
        FreeTreeNode(t);
    }
}

void ClearSampleTree(CTreeNode *t)
{
    DeleteTree(t);
    t = NULL;
}


void CountLeaf(CTreeNode* t,int& count)
{
    if(t!=NULL)
    {
        CountLeaf(t->Left(),count);
        CountLeaf(t->Right(),count);
        if(t->Left()==NULL&&t->Right()==NULL)
            count++;
    }
}

int Depth(CTreeNode* t)
{
    int depLeft,depRight,depRtv;
    if(t==NULL)
        depRtv = 0;
    else
    {
        depLeft = Depth(t->Left());
        depRight = Depth(t->Right());
        depRtv = max(depLeft,depRight)+1;
    }
    return depRtv;

}

void IndentBlanks(int num)
{
    for(int i=0;i<num;++i)
        cout<<" ";
}
const int INDENTBLANKS = 6;
void PrintTree(CTreeNode* t,int level)
{//逆时针旋转'打印树
    if(t!=NULL)
    {
        PrintTree(t->Right(),level+1);
        IndentBlanks(level*INDENTBLANKS);
        cout<<t->data<<endl;
        PrintTree(t->Left(),level+1);
    }
}

CTreeNode* CopyTree(CTreeNode* t)
{
    CTreeNode *newnode,*newlptr,*newrptr;
    if(t==NULL)
        return NULL;
    if(t->Left()!=NULL)
        newlptr = CopyTree(t->Left());
    else
        newlptr = NULL;
    if(t->Right()!=NULL)
        newrptr = CopyTree(t->Right());
    else
        newrptr = NULL;
    newnode = GetTreeNode(t->data,newlptr,newrptr);
    return newnode;
}

void LevelTravel(CTreeNode* t)
{
    queue<CTreeNode*> q1;
    CTreeNode *p;
    q1.push(t);
    while(!q1.empty())
    {
        p = q1.front();
        q1.pop();
        cout<<p->data<<endl;
        if(p->Left()!=NULL)
            q1.push(p->Left());
        if(p->Right()!=NULL)
            q1.push(p->Right());

    }
}
int main()
{
    CTreeNode *root = NULL;
    root = MakeSampleTree();
    CTreeNode *root1 = CopyTree(root);
    PostOrder(root);
    int leafCount=0;
    CountLeaf(root,leafCount);
    cout<<"叶子数: "<<leafCount<<" 深度:"<<Depth(root)<<endl;
    PrintTree(root,0);
    cout<<"拷贝后: "<<endl;
    PrintTree(root1,0);
    cout<<"层序遍历: "<<endl;
    LevelTravel(root);
    ClearSampleTree(root);
    system("pause");
    return 0;
}


复制代码



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/07/20/1247018.html,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
存储 C++ 容器
【C++&数据结构】二叉树(结合C++)的经典oj例题 [ 盘点&全面解析 ](24)
【C++&数据结构】二叉树(结合C++)的经典oj例题 [ 盘点&全面解析 ](24)
|
22天前
|
存储 算法 数据管理
C++中利用随机策略优化二叉树操作效率的实现方法
C++中利用随机策略优化二叉树操作效率的实现方法
74 1
|
23天前
|
算法 C++ 开发者
【C/C++ 数据结构 】二叉树基本性质:具有n个结点的完全二叉树的深度为[log2n]+1或者[log2(n+1)]...
【C/C++ 数据结构 】二叉树基本性质:具有n个结点的完全二叉树的深度为[log2n]+1或者[log2(n+1)]...
11 0
|
23天前
|
存储 算法 C语言
【C/C++ 数据结构 树】探索C/C++中的二叉树:从理论到实践
【C/C++ 数据结构 树】探索C/C++中的二叉树:从理论到实践
60 0
|
26天前
|
存储 算法 数据管理
【C++入门到精通】C++入门 ——搜索二叉树(二叉树进阶)
在C++中,本文介绍了搜索二叉树(二叉搜索树,BST)的概念和基本操作,包括搜索、插入和删除。搜索操作从根节点开始,按值大小决定左右查找;插入操作找到合适位置新建节点;删除操作需考虑无子节点、单子节点和双子节点的情况。文中还提供了非递归和递归实现的C++代码示例。此外,讨论了搜索二叉树在K模型和KV模型中的应用以及性能分析,强调了保持树平衡的重要性。
15 0
|
1月前
|
C++
C++进阶--搜索二叉树
C++进阶--搜索二叉树
|
2月前
|
监控 算法 测试技术
【动态规划】【树形dp】【C++算法】968监控二叉树
【动态规划】【树形dp】【C++算法】968监控二叉树
|
3月前
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
27 0
力扣C++|一题多解之数学题专场(2)
|
3月前
|
算法 C++ Python
Golang每日一练(leetDay0052) 寻找旋转排序数组中的最小值I\II
Golang每日一练(leetDay0052) 寻找旋转排序数组中的最小值I\II
33 0
Golang每日一练(leetDay0052) 寻找旋转排序数组中的最小值I\II
|
3月前
|
存储 C++ 容器
『 C++ 』二叉树进阶OJ题(下)
『 C++ 』二叉树进阶OJ题(下)