zoukankan      html  css  js  c++  java
  • 二叉树的基本使用

    创建树。前序遍历,中序遍历,后序遍历。查找二叉树结点个数,查找二叉树叶子结点个数,查找二叉树度数为1的结点的个数



    #include "iostream"
    using namespace std;
    
    struct tree
    {
        int data;
        tree *left,*right;
    };
    
    class Tree
    {
        static int n;
        static int m;
    public:
        tree *root;
        Tree()
        {
            root=NULL;
        }
        void create_Tree(int);
        void preorder(tree *);
        void inorder(tree *);
        void postorder(tree *);
        int count(tree *);
        int findleaf(tree *);
        int findnode(tree *);
    };
    
    int Tree::n=0;
    int Tree::m=0;
    
    void Tree::create_Tree(int x)
    {
        tree *t;
        t=new(tree);
        t->data=x;
        t->left=t->right=NULL;
    
        if (root==NULL)
            root=t;
        else
        {
            tree *last;
            tree *now=root;
            while (now!=NULL)
            {
                last=now;
                if (x<now->data)
                    now=now->left;
                else
                    now=now->right;
            }
            if (x<last->data)
                last->left=t;
            else
                last->right=t;
        }
    }
    
    int Tree::count(tree *p)
    {
        if (p==NULL) return 0;
        else
            return count(p->left)+count(p->right)+1;
    }
    
    int Tree::findleaf(tree *p)
    {
        if (p==NULL) return 0;
        else
        {
            if (p->left==NULL && p->right==NULL) return n+=1;
            else
            {
                findleaf(p->left);
                findleaf(p->right);
            }
            return n;
        }
    }
    
    int Tree::findnode(tree *p)
    {
        if (p==NULL) return 0;
        else
        {
            if (p->left!=NULL && p->right!=NULL)
            {
                findnode(p->left);
                findnode(p->right);
            }
            if (p->left!=NULL && p->right==NULL)
            {
                m+=1;
                findnode(p->left);
            }
            if (p->left==NULL && p->right!=NULL)
            {
                m+=1;
                findnode(p->right);
            }
        }
        return m;
    }
    
    void Tree::preorder(tree *p)
    {
        if (p!=NULL)
        {
            cout<<p->data<<" ";
            preorder(p->left);
            preorder(p->right);
        }
    }
    
    void Tree::inorder(tree *p)
    {
        if (p!=NULL)
        {
            preorder(p->left);
            cout<<p->data<<" ";
            preorder(p->right);
        }
    }
    
    void Tree::postorder(tree *p)
    {
        if (p!=NULL)
        {
            postorder(p->left);
            postorder(p->right);
            cout<<p->data<<" ";
        }
    }
    int main()
    {
        Tree A;
        int n,x;
        cout<<"输入点个数:";
        cin>>n;
        cout<<"输入"<<n<<"个数字: ";
        while (n--)
        {
            cin>>x;
            A.create_Tree(x);
        }
    
        cout<<"二叉树结点个数:"<<A.count(A.root)<<endl;
        cout<<"二叉树叶子节点个数:"<<A.findleaf(A.root)<<endl;
        cout<<"二叉树中度数为1的结点的数量为:"<<A.findnode(A.root)<<endl;
        cout<<endl<<"先序遍历序列:"<<endl;
        A.preorder(A.root);
        cout<<endl<<"中序遍历序列:"<<endl;
        A.inorder(A.root);
        cout<<endl<<"后序遍历序列:"<<endl;
        A.postorder(A.root);
    
        return 0;
    }
    


  • 相关阅读:
    trident介绍
    Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】
    tslib移植笔记(1)【转】
    jz2440-linux3.4.2-kernel移植【学习笔记】【原创】
    Linxu内核版本号后面多出字符串或者+号【学习笔记】
    向linux内核版本号添加字符/为何有时会自动添加"+"号或者"xxx-dirty"【转】
    chrome浏览器新建标签打开页面【学习笔记】
    jz2440-uboot-201204版本移植【学习笔记】【原创】
    Ubuntu 14.04 下安装 TFTP 艰辛之路【转】
    更改UBoot实现通过loady命令下载代码【转】
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7132665.html
Copyright © 2011-2022 走看看