zoukankan      html  css  js  c++  java
  • 二叉树链式存储结构

    • 二叉链表的C语言描述

    • 基本运算的算法——建立二叉链表、先序遍历二叉树、中序遍历二叉树、后序遍历二叉树、后序遍历求二叉树深度

    #include<iostream>
    #include<cstdio>
    using namespace std;
    class Tree
    {
    private:
        struct Node
        {
            char data;
            Node * lchild;
            Node * rchild;
            Node()
            {
                lchild = NULL;
                rchild = NULL;
            }
            Node(char a)
            {
                data = a;
                lchild = NULL;
                rchild = NULL;
            }
        };
        void creatTree(Node* &head)
        {
    
            char a;
            cin>>a;
            if(a == '#')
            {
                head->lchild = head->rchild = NULL;
                head = NULL;
            }
            else
            {
                head->data = a;
                head->lchild = new Node();
                head->rchild = new Node();
                creatTree(head->lchild);
                creatTree(head->rchild);
            }
        }
        void NLR(Node *head)
        {
            if(head)
            {
                cout<<head->data;
                NLR(head->lchild);
                NLR(head->rchild);
            }
            else cout<<"#";
        }
        void LNR(Node *head)
        {
            if(head)
            {
                LNR(head->lchild);
                cout<<head->data;
                LNR(head->rchild);
            }
            else cout<<"#";
        }
        void LRN(Node * head)
        {
            if(head)
            {
                LRN(head->lchild);
                LRN(head->rchild);
                cout<<head->data;
            }
            else cout<<"#";
        }
    public:
        Node * head;
        Node * cur;
        Tree() {}
        Tree(char a)
        {
            Node * tem = new Node(a);
            head = tem;
            cur = tem;
    
        }
        void creatTree()
        {
            head = new Node();
            creatTree(head);
        }
        void NLR()
        {
            if(head)
                NLR(head);
            else cout<<"#";
        }
        void LNR()
        {
            if(head)
                LNR(head);
            else cout<<"#";
        }
        void LRN()
        {
            if(head)
            {
                LRN(head);
            }
            else cout<<"#";
        }
        int BiTreeDeep(Node * head)
        {
            
            int dept = 0;
            if(head)
            {
                int lchilddept = BiTreeDeep(head->lchild);
                int rchilddept = BiTreeDeep(head->rchild);
                dept = lchilddept >= rchilddept ? (lchilddept + 1) : (rchilddept + 1);
            }
            return dept;
        }
    };
    int main()
    {
        Tree dusk;
        dusk.creatTree();
        int cnt = 0;
        dusk.NLR();
        cout<<endl;
        dusk.LNR();
        cout<<endl;
        dusk.LRN();
        cout<<endl;
        cout<<dusk.BiTreeDeep(dusk.head);
    }
    

      

  • 相关阅读:
    关于CTeX的几个大坑
    latex与word之间的各种转化方法
    事件的三种绑定方式
    实例操作JSONP原理
    Content Security Policy 入门教程
    video.js
    Javascript面向对象类文章目录
    javaScript的原型继承与多态性
    javaScript的变量
    document.documentElement.clientHeight 与 document.body.clientHeight(杜绝千篇一律的抄袭!!)
  • 原文地址:https://www.cnblogs.com/Duskcl/p/3777400.html
Copyright © 2011-2022 走看看