I. binary tree traversal. since recursive algorithms are very simple, we will not mention them here. We will mainly look at non-recursive algorithms (in fact, stack implementation, because recursion itself is a stack. sequential traversal: Thought: (1) traverse the left subtree of the current node from the root node, traverse the access side, and press into the stack (2 ). access the right subtree of the top node of the current stack, and then return
I. binary tree traversal. since recursive algorithms are very simple, we will not mention them here. We will mainly look at non-recursive algorithms (in fact, stack implementation, because recursion itself is a stack. sequential traversal: Thought: (1) traverse the left subtree of the current node from the root node, traverse the access side, and press into the stack (2 ). access the right subtree of the top node of the current stack, and then return
I. binary tree traversal.
Since recursive algorithms are very simple, we will not mention them here. We will mainly look at non-recursive algorithms (in fact, stack implementation, because recursion itself is a kind of stack)
1. Sequential traversal:
Idea: (1) traverse the left subtree of the current node from the root node in sequence, traverse access at the edge, and press into the stack
(2) access the right subtree of the current top node of the stack and return to (1) until the stack is empty.
# Define maxsize 100 typedef struct {Bitree Elem [maxsize]; int base, top;} SqStack; void PreOrderUnrec (Bitree t) {SqStack s; StackInit (s); p = t; while (p! = Null |! StackEmpty (s) {while (p! = Null) // traverse the left subtree {visite (p-> data); push (s, p); p = p-> lchild;} // endwhile if (! StackEmpty (s) // implement right subtree traversal through the embedded while in the next loop {p = pop (s); p = p-> rchild ;} // endif} // endwhile} // PreOrderUnrec
2. Sequential traversal:
Idea: (1) traverse the left subtree from the root node and traverse the edge into the stack
(2) pop up the top element of the stack and access it. Then access the right subtree at the top of the current stack and return to (1)
# Define maxsize 100 typedef struct {Bitree Elem [maxsize]; int base, top;} SqStack; void InOrderUnrec (Bitree t) {SqStack s; StackInit (s); p = t; while (p! = Null |! StackEmpty (s) {while (p! = Null) // traverse the left subtree {push (s, p); p = p-> lchild;} // endwhile if (! StackEmpty (s) {p = pop (s); visite (p-> data); // access the root node p = p-> rchild; // implement the right subtree traversal through the next loop} // endif} // endwhile} // InOrderUnrec
3. Post-order traversal (in fact, the code is from Baidu): (you need to set a flag to indicate whether the right subtree of the current node is accessed)
# Define maxsize 100 typedef enum {L, R} tagtype; // tag type. If it is R, it indicates the typedef struct {Bitree ptr; tagtype tag;} stacknode of the current node; typedef struct {stacknode Elem [maxsize]; int base, top;} SqStack; void PostOrderUnrec (Bitree t) {SqStack s; stacknode x; StackInit (s); p = t; do {while (p! = Null) // traverse the left subtree {x. ptr = p; x. tag = L; // tag as left subtree push (s, x); p = p-> lchild;} while (! StackEmpty (s) & s. elem [s. top]. tag = R) // If {x = pop (s); p = x. ptr; visite (p-> data); // if the tag is R, the access to the right subtree is complete, so the access to the root node} if (! StackEmpty (s) {s. elem [s. top]. tag = R; // traverse the right subtree p = s. elem [s. top]. ptr-> rchild;} while (! StackEmpty (s);} // PostOrderUnrec
Ii. Clue Binary Tree:
A Binary Tree Containing n nodes has a total of 2n pointer fields and n + 1 are in the Null state. In order to avoid waste of space, these NULL pointer fields can point to the front-end or successor nodes of various traversal of a binary tree. This facilitates searching for each element without having to traverse it, saving time.
1. Storage Structure:
Typedef enum {Link, Thread} PointerThr; // Link = 0: pointer, Thread = 1: clue typedef struct BiThrNode {TElemType data; Struct BiThrNode * lchild, * rchild; // left and right child pointer PointerThr LTag, RTag; // left and right signs. When LTag = Thread, it indicates a clue. When Link is used, it indicates pointing to the next node} BiThrNode, * BiThrTree;
1)If a node has a left subtree, lchild points to its left child;
Otherwise, lchild points to its direct precursor (clue );
2). If a node has a right subtree, rchild points to its right child;
Otherwise, rchild points to its successor (that is, clues );
Example:
2. The central order traversal algorithm of the clue Binary Tree:
Status IOTraver_T (BiThrTree T, Status (* Visit) (TElemType e) {// T points to the header node, and the left link lchild of the header node points to the root node, the non-recursive algorithm of the central traversal // binary clue tree T, which calls the Visit function for each data element. P = T-> lchild; // p points to the root node while (p! = T) {// when the tree is null or the traversal ends, p = T while (p-> LTag = Link) p = p-> lchild; if (! Visit (p-> data) return ERROR; // access its left subtree empty node while (p-> RTag = Thread & p-> rchild! = T) {p = p-> rchild; Visit (p-> data);} // access the successor node p = p-> rchild;} return OK;} // IOTraver_T
3. Generation Algorithm of the clue Binary Tree:
Void InThreading (BiThrTree p) // In the middle order and clues {if (p) {InThreading (p-> lchild); // left subtree for clues if (! P-> lchild) {p-> LTag = Thread; p-> lchild = pre;} // if (! Pre-> rchild) {pre-> RTag = Thread; pre-> rchild = p;} // pre = p; // keep the pre-directed p's precursor InThreading (p-> rchild); // right subtree clue} // InThreading
Status InorderThreading (BiThrTree & Thrt, BiThrTree T) {// traverses binary tree T in the middle order, and traces the result in order. Thrt points to the header node. if (! (Thrt = (BiThrTree) malloc (sizeof (BiThrnode) exit (OVERFLOW); Thrt-> LTag = Link; Thrt-> RTag = Thead; // create the header node Thrt-> rchild = Thrt; // The right pointer refers back to if (! T) Thrt-> lchild = Thrt; // If the binary tree is empty, the left pointer indicates else {Thrt-> lchild = T; pre = Thrt; // connect the header node to the treeInThreading (T );// Perform middle-order traversal to generate middle-order clues. Call the preceding function pre-> rchild = Thrt; pre-> RTag = Thread; // Thrt-> rchild = pre;} return OK;} // InOrderThreading