求二叉樹的最大高度完整代碼C++__C++

來源:互聯網
上載者:User

我這個和之前寫的模板用的是一個,這裡求了二叉樹的高度,寬度沒寫出來,

大家就勉強看一下吧

#include <iostream>

#include <string>

#include <queue>

using namespace std;

int maxWidth=2;

typedef int KeyType;

class BinSTree {

public:

    KeyType data;

    BinSTree *root;

    BinSTree *leftchild;

    BinSTree *rightchild;

    BinSTree() {

        root=NULL;

        leftchild=NULL;

        rightchild=NULL;

    }

    ~BinSTree() {

        

    }

    BinSTree *BSTreeSearch(KeyType k,BinSTree *&p);

    void BSTreeInsert(KeyType k);

    int Height(BinSTree *a);

    int BSTreeLevelTraverse(BinSTree *bt);

};

BinSTree *BinSTree::BSTreeSearch(KeyType k,BinSTree *&p)

{

    BinSTree *q=NULL;

    q=root;

    while(q)

    {

        p=q;

        if(q->data==k)

            return p;

        else if(q->data>k)

        q=q->leftchild;

        else

            q=q->rightchild;

    }

    return NULL;

}

void BinSTree::BSTreeInsert(KeyType k)

{

    BinSTree *p=NULL,*q=NULL;

    q=root;

    if(BSTreeSearch(k,p)==NULL)//尋找失敗時才插入

    {

        BinSTree *r=new BinSTree;

        r->data=k;

        if(q==NULL)//根結點為空白

        {

            root=r;

            return ;

        }

        if(p&&k<p->data)

            p->leftchild=r;

        else if(p&&k>p->data)

            p->rightchild=r;

    }

}

int BinSTree::Height(BinSTree *a) {       //求二叉樹的最大高度

    if(a==NULL)

        return 0;

    int lheight=Height(a->leftchild);

    int rheight=Height(a->rightchild);

    return max(lheight,rheight)+1;

}

int BinSTree::BSTreeLevelTraverse(BinSTree *bt)     //求二叉樹的最大寬度,沒做出來。。。

{

    deque<BinSTree*> q;

    int maxWidth = 1;

    q.push_back(bt);

    while(true) {

        int length=q.size();

        cout<<length<<endl;

        if(length=0)

            break;

        while(length>0) {

            BinSTree* pTemp=q.front();

            q.pop_front();

            --length;

            if(pTemp->leftchild)

                q.push_back(pTemp->leftchild);

            if(pTemp->rightchild)

                q.push_back(pTemp->rightchild);

        }

        maxWidth=maxWidth>q.size()?maxWidth:q.size();

    }

    return maxWidth;

}

int main() {

    int a[13]={34,18, 76,13,12,11,52, 82,16, 67,58, 73,72 };

    BinSTree bst;

    for(int i=0;i<13;i++)

    {

        bst.BSTreeInsert(a[i]);

    }

    cout<<bst.Height(bst.root)<<endl;

    //cout<<bst.BSTreeLevelTraverse(bst.root)<<endl;

    //cout<<maxWidth<<endl;

    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.