Threaded binary Tree

Source: Internet
Author: User

A threaded binary tree refers to the "rearrangement" of nodes in a two-fork tree in a logical sense, allowing it to access each node in a linear manner.
pre-sequence traversal thread:Method: Use the empty left pointer field in the node to point to the successor node. Traversal of the binary tree, the result is: 1 2 4 8 9 5 10 3 6 7,Red Linesection that indicates the successor element of the node at the time of the previous sequence traversal.
Algorithmic thinking: Initializing position pointers?p = NULL; pre-order Traversal binary tree?If p is not null, P->left points to the current node and P is null?If the left sub-tree of the current node is empty, point p to the current node
Implementation code:
<pre name= "code" class= "CPP" >/* global variable, always points to the node just visited */btree_node* p = null;void thread_via_tree (btree_node* root) {if (Root = NULL) {  /*p retains its precursor element *  /if (P! = NULL)  {   p->left = root;   p = NULL;  } /* global variable, always point to the node you just accessed */btree_node* p = null;void thread_via_tree (btree_node* root) {if (root = NULL) {  /*p retains its precursor element */< C7/>if (P! = NULL)  {   p->left = root;   p = NULL;  }   /* The left child pointer field for this node is empty, and the  pointer field that records this node is prepared to point to its successor element  to the next node that enters the function, that is, its successor, *  /if (root->left = = NULL)  {   p = root;  }   Thread_via_tree (root->left);  Thread_via_tree (Root->right); }}


can be extended:The middle sequence traversal is threaded, the post-order traversal is threaded, and the right child pointer field is used to point to its predecessor, not just the left child pointer field.
linked list thread:Algorithm idea: initialization order table list; pre-order Traversal binary tree, the current node is inserted into the sequence list in the traversal process
Implementation code: void Thread_via_list (btree_node* root, seqlist* list) {if (root = null) && (list! = null)) {  Seqlist_ins ERT (list, (seqlistnode*) root, Seqlist_length (list));  Thread_via_list (Root->left, list);  

comparison of two methods of clues:
The method of using nodal null pointers to lead to the destruction of tree structure, and can not be restored. For this problem can be added to the tree node in a threaded pointer to be resolved, but the addition of the cue pointer will be wasted memory space, not flexible compared with the use of nodal null pointer to the method, the use of linked list of clues to the method does not destroy the structure of the tree, do not need to lead to destroy the linked list, And the two-fork tree can be threaded in any sort of traversal order. So we generally consider using linked list to be used as a clue
Note:?Calling a function is best not to rely on global variables


Threaded binary Tree

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.