1. 程式人生 > >二叉樹的簡單實現

二叉樹的簡單實現

#include<iostream>
#include<stdio.h>
#include<malloc.h>

using namespace std;
#define ElemType char
#define MaxSize 100

typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode;


void CreateBTree1(BTNode *&b,char *str)//由陣列直接建立二叉樹
{
    BTNode *St[MaxSize];//St作為順序棧
    BTNode *p;
    int top = -1;//top為棧頂指標
    int j = 0;
    int k ;
    char ch;
    ch = str[j];
    b = NULL;//初始二叉鏈為空
while(ch != '\0')//迴圈str
{
    switch(ch)
    {
        case '(':
            top++;
           St[top] = p;
           k = 1; 
           break;//開始處理左孩子結點,
       case ')':
          top--;
          break;//棧頂結點的子樹處理完畢  
      case ',':
         k = 2;
         break;//開始處理右孩子結點
     default://創立一個結點,p指向它
         p = (BTNode*)malloc(sizeof(BTNode));
         p->data = ch;//存放結點值
         p->lchild = p->rchild = NULL;//左右孩子為空
         if(b == NULL)//若尚未建立頭結點
         {
            b = p;//p所指即為根結點
         }
        else
        {
         switch(k)
         {
             case 1:
                 St[top]->lchild = p;
                 break;//新建結點為根結點左孩子
            case 2:
                 St[top]->rchild = p;
                 break;//新建結點為根結點右孩子
         }

       }
}
         j++;
        ch = str[j];
}
}

void CreateBTree2(BTNode *&b)//手動輸入二叉樹
{
b = NULL;
char ch;
ch = getchar();
if(ch == '#')
{
b = NULL;
}
else
{
b = (BTNode *)malloc(sizeof(BTNode));
b->data = ch;
CreateBTree2(b->lchild);
CreateBTree2(b->rchild);
}
}

void DispLeaf(BTNode *b)//Print All Leaf
{
if(b != NULL)
{
if(b->lchild == NULL && b->rchild == NULL)
{
cout<<b->data;
}
DispLeaf(b->lchild);//lchild leaf
DispLeaf(b->rchild);//rhcild leaf

}
}

 

BTNode* FindNode(BTNode *b,ElemType x)//return a Node
{
BTNode *p;
if(b == NULL)
{
return NULL;
}
else if(b->data == x)
{
return b;
}
else
{
p = FindNode(b->lchild,x);
if(p != NULL)
{
return p;
}
else
{
return FindNode(b->rchild,x);
}
}
}

BTNode* LchildNode(BTNode *p)//return p lchild
{
return p->lchild;
}

BTNode* RchildNode(BTNode *p)
{
return p->rchild;
}

int BTHeight(BTNode *b)
{
int lchildh,rchildh;
if(b == NULL)
{
return (0);
}
else
{
lchildh = BTHeight(b->lchild);
rchildh = BTHeight(b->rchild);
return (lchildh >rchildh) ? (lchildh + 1): (rchildh + 1);
}
}

int Level(BTNode *b,ElemType x,int h)
{

int level;
if(b == NULL)
{
return (0);
}
else if(b->data == x)
{
return h;
}
else
{
level = Level(b->lchild,x,h+1);
if(level != 0)
{
return level;
}
else
{
return (Level(b->rchild,x,h+1));
}
}
}
void DispBTree(BTNode *b)
{
if(b != NULL)
{
cout<<" "<<b->data;
if(b->lchild != NULL || b->rchild != NULL)
{
cout<<"(";//有孩子結點才輸出(
DispBTree(b->lchild);//掃描左子樹
if(b->rchild != NULL)//有右子樹才輸出,
{
cout<<",";
}
DispBTree(b->rchild);//遞迴處理右子樹
cout<<")";//有孩子結點才輸出
}
}
}

void Lnodenum(BTNode *b,int h,int k,int &n)
{
if(b == NULL)
{
return ;
}
else
{
if(h == k) n++;
else if(h < k)
{
Lnodenum(b->lchild,h+1,k,n);
Lnodenum(b->rchild,h+1,k,n);
}
}
}

void PreOrder(BTNode *b)
{
if(b != NULL)
{
cout<<" "<<b->data;
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}

void PreOrder1(BTNode *b)
{
BTNode *st[MaxSize];
BTNode *p;
int top = -1;
if(b != NULL)
{
top ++;
st[top] = b;
while(top >-1)
{
p = st[top];
top--;
cout<<" "<<p->data;

if(p->rchild != NULL)
{
top++;
st[top] = p->rchild;
}
if(p->lchild != NULL)
{
top++;
st[top] = p->lchild;
}
}
cout<<endl;
}
}
/*
void PreOrder2(BTNode *b)
{
BTNode *st[MaxSize];
BTNode *p;
int top =0;
p = b;
while( p!= NULL)
{
while(p != NULL)
{
cout<<p->data;
top++;+
st[top] = p;
p = p->lchild;
}
if(top != -1)
{
p = st[top];
top--;
p = st[top];
p = p ->rchild;
}

}
cout<<endl;
}
*/
void InOrder(BTNode *b)
{
if(b != NULL)
{
InOrder(b->lchild);
cout<<" "<<b->data;
InOrder(b->rchild);
}
}

void InOrder1(BTNode *b)
{
BTNode *st[MaxSize];
BTNode *p;
int top = -1;
if( b != NULL)
{
p = b;
while(top > -1 || p != NULL)
{
while(p != NULL)
{
top ++;
}
}
}
}

void PostOrder(BTNode *b)
{
if(b != NULL)
{
PostOrder(b->lchild);
PostOrder(b->rchild);
cout<<" "<<b->data;
}
}

void DestoryBTree(BTNode *&b)
{
if(b != NULL)
{
DestoryBTree(b->lchild);
DestoryBTree(b->rchild);
free(b);
}
}

//非遞迴遍歷


int main()
{
//~~~~~~~~~~~~~~Create1
BTNode *b,*p,*lp,*rp;
cout<<"BTree"<<endl;
cout<<"create BTree"<<endl;
char str[] ="A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
CreateBTree1(b,str);
cout<<"PrintL: ";
DispBTree(b);
cout<<endl;
cout<<"H Node"<<endl;
p = FindNode(b,'H');
if(p!=NULL)
{
lp = LchildNode(p);
if(lp!=NULL)
{
cout<<" lchild is "<<lp->data;
}
else
{
cout<<"No lchild !"<<endl;
}
rp = RchildNode(p);
if(rp!=NULL)
{
cout<<" rchild is "<<rp->data;
}
else
{
cout<<"NO rchild !"<<endl;
}
}
cout<<endl;
cout<<"BTree Height is "<< BTHeight(b)<<endl;
//~~~~Order!~~~
cout<<"PreOrder -> ";
PreOrder(b);
cout<<endl;

cout<<"PreOrder1 -> ";
PreOrder1(b);`````````````````````````````````````````````````````
cout<<endl;

 

cout<<"InOrder -> ";
InOrder(b);
cout<<endl;
cout<<"PostOrder -> ";
PostOrder(b);
cout<<endl;
cout<<"Leaf Node-> ";
DispLeaf(b);
cout<<endl;

cout<<"H level is:"<<Level(b,'H',1)<<endl;
int n = 0 ;
int k = 4;
cout<<"Leval 4 nodes: ";
Lnodenum(b,1,k,n);
cout<<n<<endl;
cout<<"Free"<<endl;
DestoryBTree(b);

//~~~~~~~~~~~~~Create2
/* BTNode *b2;
cout<<"Create 2"<<endl;
CreateBTree2(b2);
cout<<"PreOrder -> ";
PreOrder(b2);
cout<<endl;
cout<<"InOrder -> ";
InOrder(b2);
cout<<endl;
cout<<"PostOrder -> ";
PostOrder(b2);
cout<<endl;
cout<<"Leaf Node-> "<<endl;
DispLeaf(b2);
cout<<"Free"<<endl;
DestoryBTree(b2);
*/

return 1;
}