To find the maximum height of the binary tree complete code c++__c++

Source: Internet
Author: User

I wrote this and the previous template used is a, here the two fork tree height, width is not written out,

Let's just take a look.

#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)//lookup fails to insert

{

Binstree *r=new Binstree;

r->data=k;

if (Q==null)//root node is empty

{

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) {//Find the maximum height of the binary tree

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)//Find the maximum width of the binary tree.

{

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;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.