Discussion threads binary tree

Threaded binary

  1. Speaking in front of the binary tree traversal, the binary tree traversal based on a certain rule (the first order, the order or subsequent) of the nodes are arranged in a linear binary sequence. This is actually a linear structure nonlinear operation, each node (except the first one and a last outer) and only one successor or predecessor in these sequences.

  2. But when the binary list in a storage structure, only to find information about the child nodes, and can not obtain a precursor and successor node information . A relatively simple way is to add two more pointers in each node a domain structure, the precursor domain pointer information save its precursor, its successor successor pointer field stored information. However, this reduces the storage density, is not suitable.

  3. Another important feature is the use of a binary list of: n + 1 must contain empty chain domain in the n binary nodes in the linked list. We can use this chain n + 1 empty field to save the predecessor and successor. Instead of leaf nodes we can come to its predecessor or successor based on a traversal features.

  4. As provides as follows: If the node has a left subtree, it lchild field indicates its left child, otherwise another lchild domain pointing to its predecessor; the same token if the node has a right child, then it rchild field indicates its right child, or another rchild field points to its successor. Of course, we need to add two flags LTag and RTag point is to distinguish between the child still around, the node is structured as follows:

    lchild LTAG data rtag rchild

    LTag = 0 (lchild domain points node left child)
    ltag =. 1 (precursor lchild domain point node)
    RTag = 0 (rchild domain points nodes right child)
    RTag =. 1 (subsequent rchild domain points to the node)

  5. In the above list of such binary structure composed of nodes as binary tree storage structure, called trail list. Wherein the pointer points to the predecessor and successor called clues, the corresponding binary referred threaded binary .

  6. The following figure shows a sequence of the binary tree and its leads in sequence list, where the solid line is a pointer (pointing to the left and right subtrees), the dashed line cues (points to the predecessor and successor). On the binary tree traversal order to somehow make it into a threaded binary process is called threading .

  7. If we want to traverse the trail binary tree, you need to find the first node in the sequence, and then turn to find a successor node, full length until a successor is empty . How to find a successor node we need to discuss here.

  8. FIG observed on, the leaf nodes rchild domain toward its successor directly , rather leaves the subsequent node, according to the laws preorder (root left and right) of FIG recombination can be drawn on a subsequent non-leaf node is the first node of its right subtree traversal sequence, i.e., the most left lower right subtree node. On the contrary, the law of the precursor find nodes in order clues species is: If the left flag is 1, then lchild domain point to its predecessor, otherwise the last node in the left subtree order traversal of its predecessor, namely the left subtree rightmost lower node.

  9. Similarly, also find a subsequent first-order or subsequent threaded binary node, as shown below is a rear threaded binary sequence to find successor node, it is also well understood.

  10. So when traversing the binary tree or look often required precursor and successor nodes using, for use as a cue list storage structure.

  11. Sometimes for convenience, storage structure modeled on a linear table, a binary tree list on the trail also add a header node, and allowed lchild binary tree root domain points, which when preorder rchild domain access point last knot point (so the link because the head node of the communication head and tail, the root node set as the left child, it needs to link the domains rchild tail node); the other hand, so that a first node of the binary tree motif sequence lchild area pointer and the pointer of the last node rchild domain point to the first node (the last precursor and a first node a successor node is the sequence of the head node). It's like the establishment of a bi-directional cue list is binary, either from Shun successor from the first node to traverse, the precursor can be lit along the traverse from the last knot.

发布了77 篇原创文章 · 获赞 19 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_42932834/article/details/93891513