數據結構學習筆記:二叉樹

Data architecture studying== Binary Tree note:

//edit by author at 2008 09 25 22:53

All of them are simple excercise.Book and internet can support enough information for us.Maybe I am just Reinventing the wheel,but Most of them are worked out  independently by myself. I think It is the biggest achivement.

==========================================================

Abstract:

1.Create Binary Tree:Use recursion and Use Queue create it by level

2.Traverse Binary Tree:Use recursion and Unrecursion(Use queue and stack),and create a sorting binary tree.

3.Destroy Binary Tree.

 

The following is some funtion source code:

 

Create Binary Tree:

  1. //=================================
  2. //Create Binary Tree and destroy it recursively
  3. pstBtNode btree_create_level(pstBtreeQue pQue)
  4. {
  5.     pstBtNode pbt=NULL , tpbt=NULL;
  6.     char ch;
  7.     pbt = new stBtNode;
  8.     if((ch=level_data[cnt++])!='/0')
  9.     {
  10.         pbt->data = ch;
  11.         pbt->mark=0;
  12.         pbt->leftchild = NULL;
  13.         pbt->rightchild = NULL;
  14.         if(queue_push(pQue, pbt)==_FALSE)
  15.         {
  16.             return NULL; //here may make memory problem!
  17.         }
  18.     }
  19.     else
  20.     {
  21.         cout<<"Create Tree Failed!"<<endl;
  22.         return pbt;
  23.     }
  24.     while((queue_is_empty(pQue)==_FALSE)&&((level_data[cnt]!='/0')))
  25.     //while(queue_is_empty(pBTQue)==_FALSE)
  26.     {
  27.         tpbt = queue_pop(pQue);
  28.         if(tpbt == NULL) 
  29.             continue;
  30.         
  31.         tpbt->leftchild = NULL;
  32.         tpbt->rightchild =NULL;
  33.         
  34.         if((ch=level_data[cnt++])!='/')
  35.         {
  36.             tpbt->leftchild=new stBtNode;
  37.             tpbt->leftchild->mark=0;
  38.             tpbt->leftchild->data = ch;
  39.             //tpbt->leftchild->leftchild = NULL;
  40.             //tpbt->leftchild->rightchild = NULL;
  41.             if(queue_push(pQue , tpbt->leftchild )==_FALSE)
  42.             {
  43.                 return NULL; //here may make memory problem!
  44.             }
  45.         }
  46.         if((ch=level_data[cnt++])!='/')
  47.         {
  48.             tpbt->rightchild=new stBtNode;
  49.             tpbt->rightchild->mark=0;
  50.             tpbt->rightchild->data = ch;
  51.             //tpbt->rightchild->leftchild = NULL;
  52.             //tpbt->rightchild->rightchild = NULL;
  53.             if(queue_push(pQue , tpbt->rightchild )==_FALSE)
  54.             {
  55.                 return NULL; //here may make memory problem!
  56.             }
  57.         }
  58.         
  59.     }
  60.     //while((queue_is_empty(pBTQue)==_FALSE)&&((level_data[cnt]=='/0')))
  61.     if(level_data[cnt]=='/0')
  62.     {
  63.         cout<<"Btree_Create_Level:Set Leaf Node as NULL Automatically!"<<endl;
  64.         while(queue_is_empty(pQue)==_FALSE)
  65.         {
  66.             tpbt = queue_pop(pQue);
  67.             tpbt->leftchild = NULL;
  68.             tpbt->rightchild =NULL;
  69.         }
  70.     }
  71.     return pbt;
  72. }
  73. #ifdef USE_RECURSION_RETURN_ROOT
  74. pstBtNode btree_create(void)
  75. {
  76.     char ch;
  77.     pstBtNode pbt;
  78. #if 0
  79.     cout<<"In put a node data:";
  80.     cin>>t;
  81. #else
  82.     if(node_data[cnt]!='/0')
  83.     {
  84.         ch=node_data[cnt++];
  85.         cout<<"btree_create:Add NULL leaf automaticly!"<<endl;
  86.     }
  87.     else
  88.         ch='/';
  89. #endif
  90.     if(ch=='/')
  91.     {
  92.         pbt = NULL;
  93.     }
  94.     else
  95.     {
  96.         pbt=new stBtNode;
  97.         pbt->data=ch;
  98.         pbt->leftchild = btree_create();
  99.         pbt->rightchild = btree_create();
  100.     }
  101.     return pbt;
  102. }
  103. #endif
  104. //-------------------------------------------------
  105. #ifdef USE_RECURSION_NO_RETURN
  106. void btree_create(pstBtNode *ppbt)
  107. {
  108.     char ch;
  109. #if 0
  110.     cout<<"In put a node data:";
  111.     cin>>ch;
  112. #else
  113.     if(node_data[cnt]!='/0')
  114.     {
  115.         ch=node_data[cnt++];
  116.         cout<<"btree_create:Add NULL leaf automaticly!"<<endl;
  117.     }
  118.     else
  119.         ch='/';
  120. #endif
  121.     
  122.     if(ch=='/')
  123.     {
  124.         (*ppbt) = NULL;
  125.         return ;
  126.     }
  127.     else
  128.     {
  129.         (*ppbt)=new stBtNode;
  130.         (*ppbt)->data=ch;
  131.         btree_create(&((*ppbt)->leftchild));
  132.         btree_create(&((*ppbt)->rightchild));
  133.     }
  134. }
  135. #endif

Traverse Binary Tree:

  1. void pre_order_stack(pstBtNode pbt , pstBtreeStack pstack)
  2. {
  3.     pstBtNode tpbt= NULL;
  4.     stack_push(pstack, pbt);
  5.     while(stack_is_empty(pstack)==_FALSE)
  6.     {
  7.         tpbt = stack_pop(pstack);
  8.         if(tpbt==NULL)
  9.         {
  10.             cout<<"-";//It's a NULL node
  11.             continue;
  12.         }
  13.         else
  14.             cout<<tpbt->data;
  15.         //if(tpbt->rightchild!=NULL)
  16.         {
  17.             if(stack_push(pstack, tpbt->rightchild)==_FALSE)
  18.             {
  19.                 return ;
  20.             }
  21.         }
  22.         //if(tpbt->leftchild!=NULL)
  23.         {
  24.             if(stack_push(pstack, tpbt->leftchild)==_FALSE)
  25.             {
  26.                 return ;
  27.             }
  28.         }
  29.     }
  30. }
  31. void in_order_stack(pstBtNode pbt , pstBtreeStack pstack)
  32. {
  33.     pstBtNode tpbt = NULL;
  34.     stack_push(pstack ,pbt->rightchild);
  35.     stack_push(pstack ,pbt);
  36.     stack_push(pstack ,pbt->leftchild);
  37.     
  38.     while(stack_is_empty(pstack)==_FALSE)
  39.     {
  40.         tpbt = stack_pop(pstack);
  41.         
  42.         if(tpbt == NULL)
  43.         {
  44.             cout<<"-";
  45.             if(stack_is_empty(pstack)==_TRUE)
  46.                 break;//pay attention here
  47.             //else
  48.             tpbt = stack_pop(pstack);
  49.             cout<<tpbt->data;
  50.             continue;
  51.         }
  52.         stack_push(pstack ,tpbt->rightchild);
  53.         stack_push(pstack ,tpbt);
  54.         stack_push(pstack ,tpbt->leftchild);        
  55.     }
  56. }
  57. void post_order_stack(pstBtNode pbt, pstBtreeStack pstack)
  58. {
  59.     pstBtNode tpbt = NULL;
  60.     stack_push(pstack ,pbt);
  61.     
  62.     while(stack_is_empty(pstack)==_FALSE)
  63.     {
  64.         tpbt = stack_pop(pstack);
  65.         
  66.         if(tpbt == NULL)
  67.         {
  68.             cout<<"-";;
  69.             continue;
  70.         }
  71.         //else
  72.         tpbt->mark++;
  73.         if(tpbt->mark == 2)
  74.         {
  75.             cout<<tpbt->data;
  76.             continue;
  77.         }
  78.         //else
  79.         stack_push(pstack , tpbt);
  80.         stack_push(pstack , tpbt->rightchild);
  81.         stack_push(pstack , tpbt->leftchild);
  82.     }
  83. }

Binary Sorting Tree:

  1. void btree_sort(char *ptab,pstBtNode *ppbt)
  2. {
  3.     pstBtNode *tpbt = NULL;
  4.     int i=0;
  5.     while(ptab[i]!='/0')
  6.     {
  7.         tpbt = ppbt;
  8.         while((*tpbt)!=NULL)
  9.         {
  10.             if(ptab[i] >= (*tpbt)->data)
  11.             {
  12.                 tpbt = &((*tpbt)->rightchild);
  13.             }
  14.             else
  15.             {
  16.                 tpbt = &((*tpbt)->leftchild);
  17.             }
  18.         }
  19.         
  20.         (*tpbt) = new stBtNode;
  21.         (*tpbt)->data = ptab[i];
  22.         (*tpbt)->leftchild = NULL;
  23.         (*tpbt)->rightchild = NULL;
  24.         i++;    
  25.     }
  26. }

 

OK.

Enjoy it.

I hope it will help you in your studing.

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章