二叉树的创建、遍历及相关操作

二叉树的创建
 //二叉树的创建
    void create() {
        string str;
        cin >> str;
        s = creat_tree(str);
    }
    node *creat_tree(string str) {
        node *p;
        if (pos > str.length())
            return NULL;
        char c = str[pos];
        if (c == '0') {
            p = NULL;
            pos++;
        } else {
            p = new node();
            p->data = c;
            num++;
            pos++;
            p->lchild = creat_tree(str);
            p->rchild = creat_tree(str);
        }
        return p;
    }
二叉树的先序遍历
//先序遍历
    void pre_order(node *p)
    {
        if(!p)
            return;
        else {
            cout << p->data ;
            pre_order(p->lchild);
            pre_order(p->rchild);
        }
    }
二叉树的中序遍历
 //中序遍历
    void in_order(node *p)
    {
        if(!p)
            return;
        else {
            in_order(p->lchild);
            cout << p->data ;
            in_order(p->rchild);
        }
    }
二叉树的后序遍历
 //后序遍历
    void last_order(node *p)
    {
        if(!p)
            return;
        else {
            last_order(p->lchild);
            last_order(p->rchild);
            cout << p->data ;
        }
    }
二叉树的层次遍历
//层次遍历输出二叉树
    void display()
    {
        a=new int[num];
        o=new char[num];
        for(int i=0;i<num;i++)
            a[i]=0;
        node *p;
        int m=0;
        a[m]=-1;
        int i=0;
        m++;
        queue<node> qu;
        qu.push(*s);
        while(!qu.empty())
        {
            p=&qu.front();
            //if(!p->lchild&&!p->rchild)  //判断是否是叶子结点
            o[i]=p->data;

            if(p->lchild) {
                qu.push(*p->lchild);
                a[m]=i;
                m++;
            }
            if(p->rchild) {
                qu.push(*p->rchild);
                a[m]=i;
                m++;
            }
            i++;
            qu.pop();
        }
        for(i=0;i<num-1;i++)
            cout<<o[i]<<' ';
        cout<<o[num-1]<<endl;
        for(i=0;i<num-1;i++)
            cout<<a[i]<<' ';
        cout<<a[num-1]<<endl;
    }
二叉树的先序遍历非递归实现
 //先序遍历非递归实现
    void Pre_order()
    {
        stack<node*> st; // 使用指向节点的指针类型的栈
        if (s == nullptr)
            return;
        st.push(s); // 将根节点指针入栈
        while (!st.empty())
        {
            node* p = st.top(); // 取栈顶指针
            st.pop(); // 弹出栈顶指针
            cout << p->data; // 输出节点数据
            if (p->rchild) {
                st.push(p->rchild); // 将右子节点指针入栈
            }
            if (p->lchild) {
                st.push(p->lchild); // 将左子节点指针入栈
            }
        }
        cout << endl;
    }
二叉树统计叶子个数
 //统计叶子结点个数
    void count(node *p)
    {
        if(!p)
            return;
        else {
            count(p->lchild);
            count(p->rchild);
            if(p->lchild&&p->rchild)
                nums++;
        }
    }
二叉树求高度/最大值
//求二叉树的高度/最大值
    int count1(node *p)
    {
        if(!p)
            return 0;
        int l1,l2;
        l1=count1(p->lchild);
        l2=count1(p->rchild);
        return p->data+max(l1,l2);   //求二叉树的最大路径
        //return 1+max(l1,l2) //求二叉树高度
    }
二叉树的叶子节点以及对应的双亲结点
 //求父子结点---树的叶子节点以及对应的双亲结点
    void search(node *p,char e)

    {
        if(!p)
            return;
        else {
            if(!p->rchild&&!p->lchild) {
                cout << p->data << ' ';
                c[n]=e;
                n++;
            }
            search(p->lchild,p->data);
            search(p->rchild,p->data);
        }
    }
完整实现代码
#include<iostream>
#include <queue>
#include<stack>
using namespace std;
/*
 * 二叉树的创建,遍历,相关计算
 */
class node {
public:
    char data;
    int weight;
    node *lchild, *rchild;

    node() {
        weight=0;
        data = '#';
        lchild = rchild = NULL;
    }
};

class tree {
public:
    int nums=1;
    char c[10];
    int n=0;
    node *s;
    int num;
    int pos;
    int *a;
    char *o;
    tree() {
        s = new node();
        num = pos = 0;
    }

    //二叉树的创建
    void create() {
        string str;
        cin >> str;
        s = creat_tree(str);
    }
    node *creat_tree(string str) {
        node *p;
        if (pos > str.length())
            return NULL;
        char c = str[pos];
        if (c == '0') {
            p = NULL;
            pos++;
        } else {
            p = new node();
            p->data = c;
            num++;
            pos++;
            p->lchild = creat_tree(str);
            p->rchild = creat_tree(str);
        }
        return p;
    }

    //先序遍历
    void pre_order(node *p)
    {
        if(!p)
            return;
        else {
            cout << p->data ;
            pre_order(p->lchild);
            pre_order(p->rchild);
        }
    }

    //中序遍历
    void in_order(node *p)
    {
        if(!p)
            return;
        else {
            in_order(p->lchild);
            cout << p->data ;
            in_order(p->rchild);
        }
    }

    //后序遍历
    void last_order(node *p)
    {
        if(!p)
            return;
        else {
            last_order(p->lchild);
            last_order(p->rchild);
            cout << p->data ;
        }
    }

    //层次遍历输出二叉树
    void display()
    {
        a=new int[num];
        o=new char[num];
        for(int i=0;i<num;i++)
            a[i]=0;
        node *p;
        int m=0;
        a[m]=-1;
        int i=0;
        m++;
        queue<node> qu;
        qu.push(*s);
        while(!qu.empty())
        {
            p=&qu.front();
            //if(!p->lchild&&!p->rchild)  //判断是否是叶子结点
            o[i]=p->data;

            if(p->lchild) {
                qu.push(*p->lchild);
                a[m]=i;
                m++;
            }
            if(p->rchild) {
                qu.push(*p->rchild);
                a[m]=i;
                m++;
            }
            i++;
            qu.pop();
        }
        for(i=0;i<num-1;i++)
            cout<<o[i]<<' ';
        cout<<o[num-1]<<endl;
        for(i=0;i<num-1;i++)
            cout<<a[i]<<' ';
        cout<<a[num-1]<<endl;
    }

    //先序遍历非递归实现
    void Pre_order()
    {
        stack<node*> st; // 使用指向节点的指针类型的栈
        if (s == nullptr)
            return;
        st.push(s); // 将根节点指针入栈
        while (!st.empty())
        {
            node* p = st.top(); // 取栈顶指针
            st.pop(); // 弹出栈顶指针
            cout << p->data; // 输出节点数据
            if (p->rchild) {
                st.push(p->rchild); // 将右子节点指针入栈
            }
            if (p->lchild) {
                st.push(p->lchild); // 将左子节点指针入栈
            }
        }
        cout << endl;
    }

    //统计叶子结点个数
    void count(node *p)
    {
        if(!p)
            return;
        else {
            count(p->lchild);
            count(p->rchild);
            if(p->lchild&&p->rchild)
                nums++;
        }
    }

    //求二叉树的高度/最大值
    int count1(node *p)
    {
        if(!p)
            return 0;
        int l1,l2;
        l1=count1(p->lchild);
        l2=count1(p->rchild);
        return p->data+max(l1,l2);   //求二叉树的最大路径
        //return 1+max(l1,l2) //求二叉树高度
    }

    //求父子结点---树的叶子节点以及对应的双亲结点
    void search(node *p,char e)

    {
        if(!p)
            return;
        else {
            if(!p->rchild&&!p->lchild) {
                cout << p->data << ' ';
                c[n]=e;
                n++;
            }
            search(p->lchild,p->data);
            search(p->rchild,p->data);
        }
    }

};

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值