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:
- //=================================
- //Create Binary Tree and destroy it recursively
- pstBtNode btree_create_level(pstBtreeQue pQue)
- {
- pstBtNode pbt=NULL , tpbt=NULL;
- char ch;
- pbt = new stBtNode;
- if((ch=level_data[cnt++])!='/0')
- {
- pbt->data = ch;
- pbt->mark=0;
- pbt->leftchild = NULL;
- pbt->rightchild = NULL;
- if(queue_push(pQue, pbt)==_FALSE)
- {
- return NULL; //here may make memory problem!
- }
- }
- else
- {
- cout<<"Create Tree Failed!"<<endl;
- return pbt;
- }
- while((queue_is_empty(pQue)==_FALSE)&&((level_data[cnt]!='/0')))
- //while(queue_is_empty(pBTQue)==_FALSE)
- {
- tpbt = queue_pop(pQue);
- if(tpbt == NULL)
- continue;
- tpbt->leftchild = NULL;
- tpbt->rightchild =NULL;
- if((ch=level_data[cnt++])!='/')
- {
- tpbt->leftchild=new stBtNode;
- tpbt->leftchild->mark=0;
- tpbt->leftchild->data = ch;
- //tpbt->leftchild->leftchild = NULL;
- //tpbt->leftchild->rightchild = NULL;
- if(queue_push(pQue , tpbt->leftchild )==_FALSE)
- {
- return NULL; //here may make memory problem!
- }
- }
- if((ch=level_data[cnt++])!='/')
- {
- tpbt->rightchild=new stBtNode;
- tpbt->rightchild->mark=0;
- tpbt->rightchild->data = ch;
- //tpbt->rightchild->leftchild = NULL;
- //tpbt->rightchild->rightchild = NULL;
- if(queue_push(pQue , tpbt->rightchild )==_FALSE)
- {
- return NULL; //here may make memory problem!
- }
- }
- }
- //while((queue_is_empty(pBTQue)==_FALSE)&&((level_data[cnt]=='/0')))
- if(level_data[cnt]=='/0')
- {
- cout<<"Btree_Create_Level:Set Leaf Node as NULL Automatically!"<<endl;
- while(queue_is_empty(pQue)==_FALSE)
- {
- tpbt = queue_pop(pQue);
- tpbt->leftchild = NULL;
- tpbt->rightchild =NULL;
- }
- }
- return pbt;
- }
- #ifdef USE_RECURSION_RETURN_ROOT
- pstBtNode btree_create(void)
- {
- char ch;
- pstBtNode pbt;
- #if 0
- cout<<"In put a node data:";
- cin>>t;
- #else
- if(node_data[cnt]!='/0')
- {
- ch=node_data[cnt++];
- cout<<"btree_create:Add NULL leaf automaticly!"<<endl;
- }
- else
- ch='/';
- #endif
- if(ch=='/')
- {
- pbt = NULL;
- }
- else
- {
- pbt=new stBtNode;
- pbt->data=ch;
- pbt->leftchild = btree_create();
- pbt->rightchild = btree_create();
- }
- return pbt;
- }
- #endif
- //-------------------------------------------------
- #ifdef USE_RECURSION_NO_RETURN
- void btree_create(pstBtNode *ppbt)
- {
- char ch;
- #if 0
- cout<<"In put a node data:";
- cin>>ch;
- #else
- if(node_data[cnt]!='/0')
- {
- ch=node_data[cnt++];
- cout<<"btree_create:Add NULL leaf automaticly!"<<endl;
- }
- else
- ch='/';
- #endif
- if(ch=='/')
- {
- (*ppbt) = NULL;
- return ;
- }
- else
- {
- (*ppbt)=new stBtNode;
- (*ppbt)->data=ch;
- btree_create(&((*ppbt)->leftchild));
- btree_create(&((*ppbt)->rightchild));
- }
- }
- #endif
Traverse Binary Tree:
- void pre_order_stack(pstBtNode pbt , pstBtreeStack pstack)
- {
- pstBtNode tpbt= NULL;
- stack_push(pstack, pbt);
- while(stack_is_empty(pstack)==_FALSE)
- {
- tpbt = stack_pop(pstack);
- if(tpbt==NULL)
- {
- cout<<"-";//It's a NULL node
- continue;
- }
- else
- cout<<tpbt->data;
- //if(tpbt->rightchild!=NULL)
- {
- if(stack_push(pstack, tpbt->rightchild)==_FALSE)
- {
- return ;
- }
- }
- //if(tpbt->leftchild!=NULL)
- {
- if(stack_push(pstack, tpbt->leftchild)==_FALSE)
- {
- return ;
- }
- }
- }
- }
- void in_order_stack(pstBtNode pbt , pstBtreeStack pstack)
- {
- pstBtNode tpbt = NULL;
- stack_push(pstack ,pbt->rightchild);
- stack_push(pstack ,pbt);
- stack_push(pstack ,pbt->leftchild);
- while(stack_is_empty(pstack)==_FALSE)
- {
- tpbt = stack_pop(pstack);
- if(tpbt == NULL)
- {
- cout<<"-";
- if(stack_is_empty(pstack)==_TRUE)
- break;//pay attention here
- //else
- tpbt = stack_pop(pstack);
- cout<<tpbt->data;
- continue;
- }
- stack_push(pstack ,tpbt->rightchild);
- stack_push(pstack ,tpbt);
- stack_push(pstack ,tpbt->leftchild);
- }
- }
- void post_order_stack(pstBtNode pbt, pstBtreeStack pstack)
- {
- pstBtNode tpbt = NULL;
- stack_push(pstack ,pbt);
- while(stack_is_empty(pstack)==_FALSE)
- {
- tpbt = stack_pop(pstack);
- if(tpbt == NULL)
- {
- cout<<"-";;
- continue;
- }
- //else
- tpbt->mark++;
- if(tpbt->mark == 2)
- {
- cout<<tpbt->data;
- continue;
- }
- //else
- stack_push(pstack , tpbt);
- stack_push(pstack , tpbt->rightchild);
- stack_push(pstack , tpbt->leftchild);
- }
- }
Binary Sorting Tree:
- void btree_sort(char *ptab,pstBtNode *ppbt)
- {
- pstBtNode *tpbt = NULL;
- int i=0;
- while(ptab[i]!='/0')
- {
- tpbt = ppbt;
- while((*tpbt)!=NULL)
- {
- if(ptab[i] >= (*tpbt)->data)
- {
- tpbt = &((*tpbt)->rightchild);
- }
- else
- {
- tpbt = &((*tpbt)->leftchild);
- }
- }
- (*tpbt) = new stBtNode;
- (*tpbt)->data = ptab[i];
- (*tpbt)->leftchild = NULL;
- (*tpbt)->rightchild = NULL;
- i++;
- }
- }
OK.
Enjoy it.
I hope it will help you in your studing.