Data Structure-clue Binary Tree

Source: Internet
Author: User

Data Structure-clue Binary Tree
Clue tree

Traversing a binary tree is to arrange the nodes in the tree into a linear sequence according to certain rules, that is, to linearly operate the non-linear structure. How can we find the direct precursor and direct successor of each node dynamically obtained during the traversal process (except for the first and last nodes )? How do I save this information? Q: How many idle pointer domains are unused for a binary tree with n nodes? If a binary tree has n nodes, there are n-1 pointer lines, and n nodes have 2n pointer fields (Lchild and Rchild ), so n + 1 idle pointer field is not used. These idle pointer fields can be used to store the direct precursor and direct successor information of the node.

Specify the pointer field of a node as follows:
◆ If a node has a left child, Lchild points to its left child. Otherwise, it points to its direct precursor;
◆ If a node has a right child, Rchild points to its right child. Otherwise, it points to its direct successor;

The node Structure of the clue Binary Tree

A binary tree storage structure consisting of such a node structure; a chain table of clues; a pointer to the node precursor and a successor is called a clue; A traversal in a certain order, and a binary tree with clues is called a clue binary tree.

Node Structure and example of a clue Binary Tree: typedef struct BiTreeNode {ElemType data; struct BiTreeNode * Lchild, * Rchild; int Ltag, Rtag;} BiThrNode;
How to find the direct successor of a node in the clue tree? Take the ordinal clue tree shown in the image as an example:

◆ If the right link of a node in the tree is a clue (Rtag = 1), the right link directly indicates the direct successor of the node, for example, the direct successor of node G is node E.
◆ If the right link of the node in the tree is a pointer (Rtag = 0 ). Based on the law of sequential traversal, the direct successor of a node with Rtag = 0 is the first node accessed when traversing its right subtree, that is, the leftmost (leaf) node in the right subtree. For example, the direct successor of node C: Find the root node F of the right subtree along the right pointer, and then go down along the left chain until the node with Ltag = 1 is the direct successor node H of node C.

Traverse the clue tree in descending order

Finding the direct successor of a node in the clue tree that traverses in the descending order is complicated and can be divided into the following three situations:

If the node is the root node of a binary tree, it is directly left blank;
If the node is the left child of its parent node and its parent node does not have the right subtree, it is directly followed by its parent node;
If the node is the left child of its parent node and its parent node has the right subtree: the direct successor is the first node that traverses the right subtree of its parent node in the descending order.

Clue-Based Binary Tree

The process of turning a binary tree into a clue binary tree in a certain traversal order.

The clue-based process is the process of modifying the NULL pointer in the traversal process to direct it to the forward or forward. Next, we will mainly discuss how to traverse the ordered binary tree in a forward order. Like the storage structure of a linear table, a head node is added to the chain table of the binary tree. The pointer field of the header node is arranged as follows:

◆ Lchild field: points to the root node of a binary tree;
◆ Rchild domain: point to the last node in the middle-order time;
◆ The Lchild pointer field of the first node in the sequence in a binary tree and the Rchild pointer field of the last node all point to the head of the header node.

A clue binary tree with a header node is added. For example, a two-way clue linked list is created for the same binary tree, A clue binary tree can be traversed from both the first node and the last Node Based on search. Obviously, this traversal does not require a stack.
# Define MAX_NODE 50 typedef enmu {Link, Thread} PointerTag;/* Link = 0 indicates pointer, Thread = 1 indicates clue */typedef struct BiThrNode {ElemType data; struct BiTreeNode * Lchild, * Rchild; PointerTag Ltag, Rtag;} BiThrNode, * BiThrTree;
Construct a binary clue tree in sequence
ElemType Nil = '#';/* with # blank */Status CreateBiThrTree (BiThrTree * T) {ElemType ch; scanf ("% c", & ch ); if (h = Nil) * T = NULL; else {* T = (BiThrTree) malloc (sizeof (BiThrNode); if (! * T) return ERROR; (* T)-> data = ch;/* generate the root node (preface) */CreateBiThrTree (& (* T)-> lchild ); /* recursively construct the left subtree */if (* T)-> lchild) (* T)-> LTag = Link; /* left child */CreateBiThrTree (& (* T)-> rchild);/* recursively construct the right subtree */if (* T)-> rchild) (* T)-> RTag = Link;/* has right children */} return OK ;}
Recursive functions with clues in the middle order traversal
BiThrNode * pre // global variable, always pointing to the accessed node void InThreading (BiThrNode * T) {if (T) {Inorder_Threading (T-> lchild) /* recursion left subtree clue */if (! T-> lchild)/* No left child */{T-> LTag = Thread;/* precursor clue */T-> lchild = pre; /* The left child Pointer Points to the precursor */} if (! Pre-> rchild)/* No right child before */{pre-> RTag = Thread;/* Subsequent clues */pre-> rchild = T; /* Forward the right child pointer to the next child (current node T) */} pre = T;/* keep the forward X/Inorder_Threading (T-> rchild) Where the pre points to T ); /* recursion of right subtree clues */}}

Clues of the front node: if (! T-> lchild) indicates that if the left pointer field of a node is null, The pre can be assigned to T-> lchild because its precursor node has just been accessed and assigned a pre value, and modify T-> LTag = Thread (defined as 1) to complete the clue of the precursor node;

Clues of the successor node: Because the successor node has not yet been accessed, it can only make a judgment on its precursor node pre's right pointer rchild, if (! Pre-> rchild) indicates that if the right pointer field of the precursor is null, T is the successor of pre, so pre-> rchild = T, and set pre-> RTag = Thread, complete the clue of the successor node.

After the judgment is completed, the current node T should be assigned to the pre for the next use.

/* Traverse the binary tree T in the middle order and make the clues in the order. Thrt points to the header node */Status InOrderThreading (BiThrTree * Thrdhead, BiThrTree T) {* Thrdhead = (BiThrTree) malloc (sizeof (BiThrNode); if (! * Thrdhead) return ERROR; (* Thrdhead)-> LTag = Link;/* Header node */(* Thrdhead)-> RTag = Thread; (* Thrdhead) -> rchild = NULL; // The right pointer is NULL if (! T) (* Thrdhead)-> lchild = * Thrdhead; // If the binary tree is empty, the left pointer returns to else {(* Thrdhead)-> lchild = T; // non-empty, pointing to the root node pre = (* Thrdhead); InThreading (T);/* traversing in the middle order for clues */pre-> rchild = * Thrdhead; // pre is the last node in the middle order traversal pre-> RTag = Thread;/* The Last node clue */(* Thrdhead)-> rchild = pre ;} return OK ;}
Clue binary tree traversal
Although the creation of the clue binary tree is complex, in the clue Binary Tree, because there are clues, in some cases, you can easily find the direct precursor or direct successor of the specified node in a traversal sequence. In addition, it is much easier to traverse a clue binary tree than to traverse a general binary tree. You do not need to set a stack and the algorithm is very concise.
/* Non-recursive algorithm for traversing the binary tree T (header node) in the central order */Status InOrderTraverse_Thr (BiThrTree T) {BiThrTree p; p = T-> lchild; /* p points to the root node */while (p! = T)/* When the empty tree or traversal ends, p = T */{while (p-> LTag = Link) p = p-> lchild; // when LTag = 0, the system cyclically goes to the first node visit (p-> data) of the central sequence; while (p-> RTag = Thread & p-> rchild! = T) {p = p-> rchild; visit (p-> data);/* access the successor node */} p = p-> rchild;} return OK ;}

Related Article

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.