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;
}