二叉树

来源:互联网 发布:如何建立网络共享盘 编辑:程序博客网 时间:2024/04/20 13:30
 

源代码

 

#include <iostream>
using namespace std ;

#define len 100

typedef struct node
{
        int data ;
        struct node * lchild , * rchild ;
} bitnode ,  * bitree ;

bitree root ;

typedef struct  queuenode
{
        bitree item ;
        struct queuenode * next ;
 }queuenode ,* queuelist;
 
 typedef struct myqueue
 {
         queuelist front ;
         queuelist rear ;
  }myqueue, * queuetype ;
 
queuetype queue ;  

typedef struct stacknode
{
    bitree item ;
    struct stacknode * next ;
}stack ;

stack * top = NULL ;

char buf[len]={0};
int ok = 0 ;
 
 
void create(bitree &t);//创建二元树
void preorder(bitree t);//递归先序遍历二元树
void inorder(bitree t);//递归中序遍历二元树
void postorder(bitree t);//递归后序遍历二元树
void lay(queuetype q,bitree t);//队列层序遍历二元树
void init(queuetype &q);//初始化队列
bitree gethead(queuetype q);//找到队列的头元素
void dequeue(queuetype q);//删除队列的头元素 
void enqueue(queuetype q,bitree temp);//队尾插入元素
int isempty_queue(queuetype q);//判断对列是否是空,空返回0 ,非空返回1


void npreorder(bitree t);//非递归先序遍历二元树
void ninorder(bitree t);//非递归中序遍历二元树 
void npostorder(bitree t);//非递归后序遍历二元树
void push(bitree item); //压堆栈
void pop(void); //弹栈
bitree gettop(void);//得到堆栈受元素
int isempty_stack(stack * top);//栈为空,返回0;否则,返回1  

int insert(int value);//插入连表,把已经访问的数据放入
int search(int value);//寻找数据,找到返回1 ,否则返回0

  int main()
  {
      init(queue);
      printf("输入要创立的二元树 :");
      create(root);

      printf("先序递归遍历二元树 : ");
      preorder(root);
      putchar('/n');

      printf("中序递归遍历二元树 : ");
      inorder(root);
      putchar('/n');

      printf("后续递归遍历二元树 : ");
      postorder(root);
      putchar('/n');
     
      printf("队列层序遍历二元树 : ");
      lay(queue,root);
      putchar('/n');
     
      printf("非递归先序遍历二元树:");
      npreorder(root);
      putchar('/n');
     
      printf("非递归中序遍历二元树:");
      ninorder(root);
      putchar('/n');
     
     
      printf("非递归后序遍历二元树:");
      npostorder(root);
      putchar('/n'); 
     
      system("PAUSE");
      return 0 ;
  }
 
void create(bitree &t)
{
     int ch = 0 ;
     scanf("%c",&ch);
     if(ch =='#' )
       t = NULL ;
     else
       {
           t = new (bitnode) ;
           t->data = ch ;
           create(t->lchild);
           create(t->rchild);
       }
 }
 
void preorder(bitree t)
{
if(t)
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}

void inorder(bitree t)
{
if(t)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}

void postorder(bitree t)
{
if(t)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}

 
void lay(queuetype q,bitree t)
{
     enqueue(q,t);
     do
     {
       t = gethead(q);
       printf("%c",t->data);
       dequeue(q);
       if(t->lchild)
       {
         enqueue(q,t->lchild);
       }
       if(t->rchild)
       {
       enqueue(q,t->rchild);
       }
      
      }while(t&&isempty_queue(q));
 }
void init(queuetype &q)
{
     queuelist head ;
     q = new (myqueue);
     head = new (queuenode);
     head->item->data = ' ';
     q->front = q->rear = head ;
     q->rear->next =NULL ;
 }

bitree gethead(queuetype q)
{
        return q->front->next->item ;
 }

void dequeue(queuetype q)
{
     queuelist temp ;
     temp = q->front ;
     q->front = q->front->next ;
    
     delete temp ;
    
 }
void enqueue(queuetype q,bitree temp)
{
     queuelist p ;
    
     p = new (queuenode);
     p->item = temp ;
     q->rear->next = p ;
     q->rear = p ;
     q->rear->next = NULL ;
    
      
 }

int isempty_queue(queuetype q)
{
    if(q->front != q->rear)
      return 1 ;
    else
      return 0 ; 
}

void npreorder(bitree t)
{
    bitree temp ;
    push(t);
    while(isempty_stack(top)==1)
    {
        while(temp = gettop())//一直走到最左边
        {
            printf("%c",temp->data);
            push(temp->lchild);
           
        } 
       
        pop();
        if(isempty_stack(top)==1)
        {
            temp = gettop();
            pop();
            push(temp->rchild);
        }    
    }   
   
}

void ninorder(bitree t)
{
    bitree temp ;
    push(t);
    while(isempty_stack(top)==1)
    {
        while(temp = gettop())//一直走到最左边
        {
            push(temp->lchild);
        } 
       
        pop();
        if(isempty_stack(top)==1)
        {
            temp = gettop();
            printf("%c",temp->data);
            pop();
            push(temp->rchild);
        }    
    }   
   
}

void npostorder(bitree t)
{
    bitree temp ;
    push(t);
    temp = t->lchild ;
    while(isempty_stack(top)==1)
    {
      
        if(temp)
        {
            push(temp);
            temp = temp->lchild ;
        } 
       
       
       
       
       
       else
         {
           
           
            if((!gettop()->rchild)||search(gettop()->rchild->data)==1)
            {
             printf("%c",gettop()->data);
             insert(gettop()->data);
             pop();
            
            
            
            
            }
            else
             
              temp = gettop()->rchild ;
          }
        }    
      
   
}
    
void push(bitree item)
{
    stack * p = new stacknode ;
    if( p == NULL)
     {
         printf("存储空间不够!/n");
         exit(0);
     }
     p->item = item ;
     p->next = top ;
     top = p ;
}    
void pop(void)
{
    stack * p = new stacknode ;
    if( p == NULL)
     {
         printf("存储空间不够!/n");
         exit(0);
     }
    
     p = top ;
     top = top->next ;
     delete p ;

bitree gettop(void)
{
    stack * p ;
    p = top ;
    return p->item ;
}
   
int isempty_stack(stack * top)
{
    if(top == NULL)
    return 0 ;
    else
    return 1 ;
}

int insert(int value)
{
 
 buf[ok] = value ;
 ok++ ;
 return ok ;
 
 }

int search(int value)
{
  int i = 0 ;
 
 for(i = 0 ;i <= ok ;i++)
  {
    if(buf[i]== value)
      return 1 ;
    
   }
  return 0 ;
 
 }

测试结果:

输入要创建的二叉树:abc##d##e#f##

先序递归遍历二元树:abcdef

中序递归遍历二元树:cbdaef

后续递归遍历二元树:cdbfea

队列层序遍历二元数:abecdf

非递归先序遍历二元树:abcdef

非递归中序遍历二元树:cbdaef

非递归后续遍历二元树:cdbfea