数据结构:二叉树

我们采用链式储存结构的方式来表示二叉树,每一个二叉树结点包含树节点的值、树的左孩子指针、树的右孩子指

针:

class BiNode{
    public:
    char data;
    struct BiNode *lchild,*rchild;
};

那么对于一个二叉树来说,只需要存放指向树根节点的指针即可,另外还需要声明二叉树的一些功能,比如遍历方法、求树高等(BiTree.h):

#ifndef BITREE_HINCLUDED
#define BITREE_HINCLUDED
#include<iostream>
#include<string>
using namespace std;
class BiNode{
    public:
    char data;
    struct BiNode *lchild,*rchild;
};

class BiTree{
    private:
    BiNode *root;
    int height;
    void pre_Order(BiNode *t);
    void in_Order(BiNode *t);
    void post_Order(BiNode *t);
    BiNode* create(string &s,int &pos);
    void get_Height(BiNode *t,int h);
    public:
    BiTree(){root=NULL;height=0;}
    //按照前序遍历序列创建二叉树
    void createBiTree(string s);
    //前序遍历序列二叉树
    void preOreder();
    //中序遍历二叉树
    void inOreder();
    //后序遍历二叉树(递归方法)
    void postOrder();
    //后序遍历二叉树(使用栈的非递归方法)
    void postOrder1();
    //层序遍历二叉树
    void levelOrder();
    //求树的高度
    int getHeight();
    //求两个节点的最大祖先
    void ancestor(char A,char B);
};

二叉树类的成员函数定义(BiTree.cpp)

#include "BiTree.h"
#include "queue"
#include "stack"
#include "vector"
#include<iostream>
using namespace std;
//递归创建二叉树,如果是#表示空节点
BiNode *BiTree::create(string &s,int &pos)
{
    ++pos;
    BiNode *t;
    if((unsigned)pos>=s.size())
        return NULL;
    else{
        if(s[pos]=='#')
            t=NULL;
        else{
            t=new BiNode;
            t->data=s[pos];
            t->lchild=create(s,pos);
            t->rchild=create(s,pos);
        }
		return t;
    }
}
//按照前序遍历序列创建二叉树
void BiTree::createBiTree(string s)
{
    int pos=-1;
    root=create(s,pos);
}
//前序遍历二叉树
void BiTree::preOrder()
{
    pre_Order(root);
    cout<<endl;
}
void BiTree::pre_Order(BiNode *t)
{
    if(t!=NULL)
    {
        cout<<t->data<<" ";
        pre_Order(t->lchild);
        pre_Order(t->rchild);
    }
}
//中序遍历
void BiTree::inOrder()
{
    in_Order(root);
    cout<<endl;
}
void BiTree::in_Order(BiNode *t)
{
 	if(t!=NULL)
    {
        in_Order(t->lchild);
        cout<<t->data<<" ";
        in_Order(t->rchild);
    }
}
//后序遍历(递归方法)
void BiTree::postOrder()
{
    post_Order(root);
    cout<<endl;
}
void BiTree::post_Order(BiNode *t)
{
    if(t!=NULL)
    {
        post_Order(t->lchild);
        post_Order(t->rchild);
        cout<<t->data<<" ";
    }
}
//后序遍历二叉树(使用栈的非递归方法)
//后序遍历先遍历左子树,再遍历右子树,最后遍历根结点
//对于一个节点而言,先一直遍历到最左节点
//然后用r 记录右子树是否遍历,如果没有遍历,则遍历右子树
void BiTree::postOrder1()
{
    //p表示当前树节点指针
    //r表示最近访问的树节点指针
    BiNode *p,*r;
	r=NULL;
    p=root;
    stack<BiNode*> my_stack;
    while(p!=NULL||!my_stack.empty()){
        if(p){
            //一直走到树的最左边
            my_stack.push(p);
            p=p->lchild;
        }
        else{
            p=my_stack.top();
            //如果右子树没有遍历,遍历右子树
            if(p->rchild!=NULL&&p->rchild!=r)
            {
                p=p->rchild;
                my_stack.push(p);
                //注意这里需要向左转,因为如果不向左转,将会遍历右子树节点两边
                p=p->lchild;
            }
            //否则遍历根节点
            else{
                p=my_stack.top();
                my_stack.pop();
                cout<<p->data<<" ";
                //更新最近遍历的节点
                r=p;
                //将遍历的节点设为NULL,进行下一个节点的遍历
                p=NULL;
            }
        }
    }
    cout<<endl;
}
//使用队列进行层序遍历二叉树
void BiTree::levelOrder()
{
    if(root==NULL)
        return;
    queue<BiNode*> q;
    q.push(root);
    while(!q.empty())
    {
        BiNode *t;
        t=q.front();
        q.pop();
        cout<<t->data<<" ";
        if(t->lchild!=NULL)
            q.push(t->lchild);
        if(t->rchild!=NULL)
            q.push(t->rchild);
    }
    cout<<endl;
}
//求树的高度
int BiTree::getHeight(){
    get_Height(root,0);
    return height;
}
void BiTree:get_Height(BiNode *t,int h)
{
    if(t!=NULL)
        ++h;
    	if(h>height)
            height=h;
    	get_Height(t->lchild,h);
    	get_Height(t->rchild,h);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值