Clue two fork Tree principle and preface, middle order Clue (Java edition) __ Clue two fork Tree

Source: Internet
Author: User
Tags static class
first, the Clue two fork tree principle

Introduction to the principle of binary tree and special binary tree The article mentions that binary trees can use two kinds of storage structure: sequential storage and two-fork list. In the process of using the two-fork-list storage structure, there will be a large number of NULL pointer fields, in order to make full use of these null pointer domain, extended "Clue two fork Tree". Review the binary chain table storage structure, as shown in the following figure:



By looking at the two-link list above, there are several null pointer fields that are not pointing. For a two-link list with n nodes, each node has 2 pointer fields pointing to the left and right nodes, and the whole binary list has 2n pointer fields. and N nodes of the two-link list has n-1 branch line, then the number of NULL pointer field =2n-(n-1) = n+1 null pointer field, from the storage space point of view, this n+1 null pointer domain wasted memory resources.
From another point of view, if we want to know in the order to traverse the binary chain table when the first node of the B-node or successor node, must be in order to traverse the binary list to know the results, each need to do the results of a traversal, Is it possible to consider early storage of this precursor and subsequent relationships to improve time efficiency?
Through the analysis of the above two aspects, we can store the pointers of the predecessors and successors of nodes in some traversal mode by making full use of the null pointer field in the binary list. We put this point to the precursor and subsequent pointers to the clues, coupled with the clues of the two-fork linked list into a clue list, the corresponding two-fork tree becomes "Clue two fork Tree (threaded Binary)" .

Second, the construction of clues two-tree process

1, we have a two-fork tree in the sequence traversal (do not understand the binary tree traversal please refer to the binary tree and special Binary tree Introduction), the right child node of all nodes are null pointer field point to its successor node. The following figure:



Through the middle-order traversal we know that H's right pointer is empty, and the successor node of H is D (as in the 1th step above), the right pointer of I is empty, and the successor node of I is B (as in the 2nd step above), and so on, knowing that the successor node of G is null, then G's right pointer points to NULL.

2. Next, the pointer field with the left pointer field of all nodes of this binary tree is pointed to its predecessor node. The following figure:



As shown above, the left pointer field of H points to null (such as step 1th), and the first node of I is D, then the left pointer to D, and so on.

Through the above two steps to complete the whole binary tree of the clue, the final result of the following figure:



By observing the image above (the blue dotted line represents the successor, the green dotted line represents the precursor), it can be seen that the clue two fork tree, is tantamount to a binary tree into a "special two-way linked list" (the following will explain why the special two-way linked list), so for our new additions, deletions, find nodes to bring convenience. So we call the process of a two-fork tree in some order to turn it into a clue two-tree. The following figure:



Careful analysis of the two-way linked list, compared with the two-fork tree after the cue, for example, node D and successor node I, after the completion of the cue, there is no direct clue pointer, but there is a parent-child node of the pointer, node A and node F, after the thread is complete, node A does not directly point to the successor node F of the clue Pointer, Instead, you can find the final node F through the parent-child node traversal, the predecessor node also has the same problem, because many nodes do not exist direct clues, so I will be called this two-way linked list "Special two-way linked list", and then use the process according to the pointer is the cue pointer or child node pointers to deal with separately, So each node needs to indicate whether the current left and right pointers are cue pointers or child node pointers, which requires modifying the node's data structure. The modified data structure is as follows:

   Class Node {
        String data;    Data field
        Node left;      Left pointer field
        Node right;     Right pointer field
        byte LeftType;  Left pointer field type 0: Point to Child node, 1: predecessor or successor thread
        byte RightType;//Right pointer field type 0: Point to Child node, 1: predecessor or successor thread
    }

The final two-fork list is modified to look like the following image:


third, the Clue two Fork Tree Code (Java version)

The following is the implementation code for the middle order threaded binary tree :

/** * @Title: two fork Tree related operations * @Description: * @Author: Uncle Ming * @Date: January 6, 2017 afternoon 2:49:14 * @Version V1.0 * * Public cl   Ass Threadbinarytree {private Node prenode;        Record the previous node//node storage structure static class Node {String data when the thread is being threaded;          data field Node left;         Left pointer field Node right;   Right pointer field Boolean isleftthread = false;  Left pointer field type false: Point to child node, true: predecessor or successor thread Boolean isrightthread = false;
        Right pointer field type false: Point to child node, true: predecessor or successor cue node (String data) {this.data = data; }/** * Constructs a binary tree (complete binary tree) through an array * @param array * @param index * @return/static Node

        Createbinarytree (string[] array, int index) {node node = null;
            if (Index < array.length) {node = new node (Array[index]);
            Node.left = Createbinarytree (Array, Index * 2 + 1);
        Node.right = Createbinarytree (Array, Index * 2 + 2);
    } return node;
 }

    /**    * Sequence-threaded binary tree * @param node nodes/void Inthreadorder (node node) {if (node = null) {R
        Eturn;

        ///Processing left subtree Inthreadorder (node.left);
            The left pointer is empty, pointing the left pointer to the predecessor node if (Node.left = null) {node.left = Prenode;
        Node.isleftthread = true; The successor node of the previous node points to the current node if (prenode!= null && prenode.right = null) {Prenode.right = n
            Ode
        Prenode.isrightthread = true;

        } prenode = node;
    Processing right subtree Inthreadorder (node.right); /** * Sequence traversal thread Two-fork tree, followed by traversal (idea: Find the leftmost child node start) * @param node/void Inthreadlist (node node) {/
        /1, find the middle sequence traversal way to start the node while (node!= null &&!node.isleftthread) {node = Node.left;

            while (node!= null) {System.out.print (Node.data + ","); If the right pointer is a clue if (node.isrightthread) {node = node.right;

            else {//If the right pointer is not a thread, find the node = node.right where the right subtree begins;
                while (node!= null &&!node.isleftthread) {node = Node.left; /** * In the sequence traversal thread two fork tree, in the precursor traversal (idea: Find the most right child node start in reverse traversal) * @param node/void I
            Nprethreadlist (node node) {//1, find the last node while (node.right!= null &&!node.isrightthread) {
        node = node.right;

            while (node!= null) {System.out.print (Node.data + ",");

            If the left pointer is a clue if (node.isleftthread) {node = Node.left;
                else {//If the left pointer is not a clue, find the node = node.left where Zuozi begins;
                while (node.right!= null &&!node.isrightthread) {node = node.right;
        /** * Pre-order threaded binary Tree * @param node/void Prethreadorder (node node) { If(node = null)
        {return;
            The///left pointer is null, pointing the left pointer to the predecessor node if (Node.left = null) {node.left = Prenode;
        Node.isleftthread = true; The successor node of the previous node points to the current node if (prenode!= null && prenode.right = null) {Prenode.right = n
            Ode
        Prenode.isrightthread = true;

        } prenode = node;
        Processing the left subtree if (!node.isleftthread) {prethreadorder (node.left);
        }//Process right subtree if (!node.isrightthread) {prethreadorder (node.right); }/** * pre-order traversal thread Two-fork tree (traversed by subsequent thread) * @param node/void Prethreadlist (node node) {while n
                Ode!= null) {while (!node.isleftthread) {System.out.print (Node.data + ",");
            node = Node.left;
            } System.out.print (Node.data + ",");
        node = node.right; }} public static void Main (string[)args) {string[] array = {"A", "B", "C", "D", "E", "F", "G", "H"};

        Node root = createbinarytree (array, 0);
        Threadbinarytree tree = new Threadbinarytree ();
        Tree.inthreadorder (root);
        SYSTEM.OUT.PRINTLN ("The Order of the successor nodes traverse the clue two fork Tree results:");
        Tree.inthreadlist (root);
        System.out.println ("\ n order by successor node traversal clue two fork Tree result:");

        Tree.inprethreadlist (root);
        Node Root2 = createbinarytree (array, 0);
        Threadbinarytree tree2 = new Threadbinarytree ();
        Tree2.prethreadorder (ROOT2);
        Tree2.prenode = null;
        System.out.println ("\ n pre-sequence traversal of clues by subsequent nodes two fork tree result:");
    Tree.prethreadlist (ROOT2);
 }
}
Iv. SummaryThe essence of the clue is to change the null pointer in the two-link list to a clue to the predecessor node or the successor node; the process of the clue is to modify the process of the hollow pointer of the binary chain list, which can be traversed by the way of the preface, the Order and the sequence, respectively, and generate different clues two fork trees; with a clue two fork tree after we iterate again, is equivalent to operating a two-way list. Usage Scenario: If we often need to traverse the binary tree or find the node's predecessor and successor nodes in the process of using the two-fork tree, we may consider using the clue two-tree storage structure.

PS: to Be Continued ...
Clue two fork Tree principle, the preface and the middle order clue the article has done as far as possible detailed description, to the second clue binary tree comparison relatively complex, in order to avoid causing discomfort, with a separate article for analysis, the need for students please turn to the second clue 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.