- Preorder traversal of the tree (non-recursive version)
void TreePreOrderByLoop(TreeNode* root){ if(root == NULL) return; //1. First push the root node into the stack SeqStack stack; SeqStackInit(&stack); SeqStackPush(&stack,root); //2. The loop starts, if the stack is empty, the loop ends TreeNode* cur = NULL; while(GetTop(&stack,&cur)){ // Take the top element of the stack as the current element // pop the stack SeqStackPop(&stack); // access the current element printf("%c ",cur->data); //Push the current right subtree onto the stack if(cur->rchild != NULL) SeqStackPush(&stack,cur->rchild); //Push the left subtree of the current element onto the stack if(cur->lchild != NULL) SeqStackPush(&stack,cur->lchild); } return; }
- Inorder traversal of the tree (non-recursive version)
void TreeInOrderByLoop(TreeNode* root){ if(root == NULL) return; SeqStack stack; SeqStackInit(&stack); //1. Define a cur pointer to point to the root node TreeNode* cur = root; while(1){ //The loop determines whether cur is empty. If it is not empty, cur is pushed onto the stack, and cur points to cur->lchild while(cur != NULL){ SeqStackPush(&stack,cur); cur = cur->lchild; } //If cur is empty, take the top element of the stack, access, pop the stack TreeNode* top = NULL; int ret = GetTop(&stack,&top); if (ret == 0) { printf("\n"); return; } printf("%c ",top->data); SeqStackPop(&stack); //Let cur point to the right subtree of the top element of the stack, and repeat the looping process cur = top->rchild; } return ; }
- Postorder traversal of the tree (non-recursive version)
void TreePostOrderByLoop(TreeNode* root){ if(root == NULL) return; SeqStack stack; SeqStackInit(&stack); //Define cur to point to root TreeNode* cur = root; TreeNode* pre = NULL; //The loop determines whether cur is empty. If it is not empty, push cur onto the stack, and cur points to cur->lchild while(1){ while(cur != NULL){ SeqStackPush(&stack,cur); cur = cur->lchild; } //If cur is empty, loop to get the top element of the stack TreeNode* top = NULL; int ret = GetTop(&stack,&top); if (ret == 0) { printf("\n"); return; } //Check the top element of the stack //a. If the rchild of top and the previous element accessed are the same element //or top->rchild is empty //Access the top element of the stack and pop the stack if(top->rchild == NULL || top->rchild == pre){ printf("%c ",top->data); SeqStackPop(&stack); pre = top; } //If the above conditions are not met, cur points to top->rchild else{ cur = top->rchild; } } return; }
- Find the mirror image (flip) of a binary tree
void Swap(TreeNode** a,TreeNode** b){ TreeNode* tmp = *a; *a = *b; *b = tmp; } void TreeMirror(TreeNode* root){ if(root == NULL) return; Swap(&root->lchild,&root->rchild); TreeMirror(root->lchild); TreeMirror(root->rchild); return; }
Non-recursive version:
void TreeMirrorByLoop(TreeNode* root){ // Layer order traversal implementation if(root == NULL) return; SeqQueue q; SeqQueueInit(&q); SeqQueuePush(&q,root); TreeNode* cur = NULL; while(SeqQueueFront(&q,&cur)){ Swap(&cur->lchild,&cur->rchild); SeqQueuePop(&q); if(cur->lchild != NULL){ SeqQueuePush(&q,cur->lchild); } if(cur->rchild != NULL){ SeqQueuePush(&q,cur->rchild); } } return; }
- Check if a tree is a complete binary tree
int IsCompleteTree(TreeNode* root){ if(root == NULL) return 0; SeqQueue q; SeqQueueInit(&q); SeqQueuePush(&q,root); TreeNode* cur = NULL; int start_step_two = 0; while(SeqQueueFront(&q,&cur)){ SeqQueuePop(&q); if(start_step_two == 0){ // stage one if(cur->lchild != NULL && cur->rchild != NULL){ // have both left and right subtrees SeqQueuePush(&q,cur->lchild); SeqQueuePush(&q,cur->rchild); } else if(cur->lchild == NULL && cur->rchild != NULL) // only right subtree return 0; else if(cur->lchild != NULL && cur->rchild == NULL){ // only left subtree start_step_two = 1; SeqQueuePush(&q,cur->lchild); } else{//No left and right subtrees start_step_two = 1; } } else{//Phase 2 if(cur->lchild == NULL && cur->rchild == NULL); else return 0; } } return 1; }
Test function:
void TestPreOrderByLoop(){ PRINT_HEAD; TreeNodeType data[] = "abd##eg###c#f##"; TreeNode* root = TreeCreate(data,sizeof(data)/sizeof(data[0])-1,'#'); printf("The preorder traversal is: "); TreePreOrderByLoop(root); printf("\n"); } void TestInOrderByLoop(){ PRINT_HEAD; TreeNodeType data[] = "abd##eg###c#f##"; TreeNode* root = TreeCreate(data,sizeof(data)/sizeof(data[0])-1,'#'); printf("Inorder traversal is: "); TreeInOrderByLoop(root); printf("\n"); } void TestPostOrderByLoop(){ PRINT_HEAD; TreeNodeType data[] = "abd##eg###c#f##"; TreeNode* root = TreeCreate(data,sizeof(data)/sizeof(data[0])-1,'#'); printf("The post-order traversal is: "); TreePostOrderByLoop(root); printf("\n"); } void TestMirror () { PRINT_HEAD; TreeNodeType data[] = "abd##eg###c#f##"; TreeNode* root = TreeCreate(data,sizeof(data)/sizeof(data[0])-1,'#'); TreeMirror(root); printf("The preorder traversal is: "); TreePreOrderByLoop(root); printf("\n"); printf("Inorder traversal is: "); TreeInOrderByLoop(root); printf("The post-order traversal is: "); TreePostOrderByLoop(root); printf("Level order traversal is: "); TreeLevelOrder(root); printf("\n"); } void TestMirrorByLoop(){ PRINT_HEAD; TreeNodeType data[] = "abd##eg###c#f##"; TreeNode* root = TreeCreate(data,sizeof(data)/sizeof(data[0])-1,'#'); TreeMirror(root); printf("The preorder traversal is: "); TreePreOrderByLoop(root); printf("\n"); printf("Inorder traversal is: "); TreeInOrderByLoop(root); printf("The post-order traversal is: "); TreePostOrderByLoop(root); printf("Level order traversal is: "); TreeLevelOrder(root); printf("\n"); } void TestIsCompleteTree(){ PRINT_HEAD; TreeNodeType data[] = "abd##eg###c#f##"; TreeNode* root = TreeCreate(data,sizeof(data)/sizeof(data[0])-1,'#'); int ret = IsCompleteTree(root); printf("ret = %d\n",ret); } int main(){ TestPreOrderByLoop(); TestInOrderByLoop(); TestPostOrderByLoop(); TestMirror (); TestMirrorByLoop (); TestIsCompleteTree(); }
Result presentation: