Data structure review notes-binary tree 2 and binary tree

Source: Internet
Author: User

Data structure review notes-binary tree 2 and binary tree

Non-recursive traversal of Binary Trees:

Non-recursive Traversal Algorithm
The basic idea for implementing non-recursive algorithms: Use stacks:

Void InOrderTraversal (BinTree BT) {BinTree T = BT; Stack S = CreatStack (MaxSize);/* Create and initialize Stack S */while (T |! IsEmpty (S) {while (T)/* always to the Left and press the node along the way into the stack */{Push (S, T); T = T-> Left ;} if (! IsEmpty (S) {T = Pop (S);/* node Pop-up stack */printf ("% 5d", T-> Data);/* (ACCESS) print node */T = T-> Right;/* Turn to the Right subtree */}}}


 
Void InOrderTraversal (BinTree BT) {BinTree T = BT; Stack S = CreatStack (MaxSize);/* Create and initialize Stack S */while (T |! IsEmpty (S) {while (T)/* always to the left and press the node along the way into the stack */{Push (S, T); printf ("% 5d ", t-> Data);/* (ACCESS) print node */T = T-> Left;} if (! IsEmpty (S) {T = Pop (S);/* node Pop-up stack */T = T-> Right;/* Turn to the Right subtree */}}}

 

Void PostOrder_NoRecurse (BinTree BT, void (* Do) (BinTree) // non-recursive post-order traversal {BinTree T = BT; Stack s = CreateStack (20 ); int Tag [20]; // Tag is used to mark the elements in the stack for the first time. It is an integer stack while (T |! Stack_IsEmpty (s) {while (T) // every time a new element is encountered, it is controlled here {Stack_Push (s, T ); // put the stack and cycle to its leftmost Tag [s-> size-1] = 0; T = T-> Left;} while (! T &&! Stack_IsEmpty (s) {T = Stack_Pop (s); // retrieves an element in the stack and determines its Tag if (Tag [s-> size]) {(* Do) (T); // Tag! = 0 indicates that the node is met for the third time and T = 0 is operated on it; // set T to 0 to trigger the While condition, continue loop} else // Tag = 0 indicates the second encounter (the first time is to set the Tag to 0) {if (! T-> Right) (* Do) (T); // if the Right son does not exist, else {Stack_Push (s, T) is output directly; // if the Right son exists, put it back into the stack Tag [s-> size-1] ++; // and add the corresponding Tag} T = T-> Right; // return the Right son. Note: If the right son does not exist, the While continue loop will be triggered} // otherwise, it will be determined that a new element will jump out of the loop and the large While loop outside will be continued }}}
Are two traversal sequences used to determine a binary tree?

Is it known that any two of the three traversal sequences can uniquely identify a binary tree? We know that, no matter which type, we must first prophet sequential traversal.

For example, the first and middle order traversal sequences are used to determine a binary tree.
Analysis 〗
1. traverse the first node of the sequence to determine the root node;
2. Split the Left and Right sub-sequences in the central traversal Sequence Based on the Root Node
3. The left and right subtree are recursively decomposed using the same method.

Exercise: paste it in a few days.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.