二叉樹的基本使用,二叉樹使用

來源:互聯網
上載者:User

二叉樹的基本使用,二叉樹使用

建立樹,前序走訪,中序遍曆,後序遍曆,尋找二叉樹結點個數,尋找二叉樹葉子結點個數,尋找二叉樹度數為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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.