Liar Data Structure 15: Clue two fork Tree

Source: Internet
Author: User
Tags thread

1. What is a clue two fork tree?

N nodes in the two-link list contains (2n-(n-1) =n+1 null pointer field. By using the null pointer field in a binary chain list, a pointer to the precursors and subsequent nodes of a node in some sort of traversal sequence is stored.

(This additional pointer is called a "clue"). This added clue to the two-link list called the Clue list, the corresponding two-fork tree is called the Clue two fork tree.

2. Why should I add a clue?

① Many null pointer fields do not store anything, it is a waste of memory resources.

② binary Chain list, we can only know each node point to its left and right child node address, but do not know the predecessor of each node is who, successor is who.

③ Clue List solves the problem that the node can not directly find the precursor and subsequent node in some traversal sequence.

④ Clue two fork tree is equal to turn a binary tree into a two-way list, so we insert the deletion of a node is very convenient.

3. What is cue?

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The process of making a two-fork tree in some order to turn it into a clue two-tree is called a cue, and the process of the thread is to modify the null pointer during the traversal process.

4. The structure of the Clue two fork tree:

Leftchild Islefttag Data Isrighttag Rightchild

Islefttag is 0 o'clock Leftchild the left child pointing to the node, and is the precursor of 1 o'clock leftchild pointing to the node;

The Isrighttag is 0 o'clock Rightchild the right child pointing to the node, for 1 o'clock Rightchild Point to the successor of the node;

Note:Islefttag,isrighttag is just a Boolean variable that holds 0 or 1 digits, Islefttag is used to differentiate between leftchild pointing to the left child or the precursor,

Isrighttag is used to differentiate between rightchild pointing to the right child or successor, which takes up less memory space than pointer variables such as Leftchild and Rightchild.

5. Java implementation Clue two fork tree:

The public class Threadtree {private node root;//root nodes private node prenode;//the cue to save the precursor/**  
        * Internal Node class * * Private class Node {private int data; Private Node leftchild; The left child nodes private node rightchild; Right child node private Boolean islefttag; Left child flag domain private Boolean isrighttag;  
            Right child flag domain public Node (int data) {this.data = data;  
            This.leftchild = null;  
            This.rightchild = null; This.islefttag = false; Lefttag is 0 o'clock to the left child pointing at the node, to 1 o'clock the precursor This.isrighttag = False that points to the node;  
        Righttag is 0 o'clock to the right child pointing to the node, and the successor to the node for 1 o'clock is public threadtree () {this.root = null;  
    This.prenode = null; /** * Recursive creation of two-fork tree * * @param node * @param data/public void Buildtree (Nod  e node, int data) {if (root = null) {root = new node (data);
        else {if (data < Node.data) {if (Node.leftchild = = null) {  
                Node.leftchild = new Node (data);  
                else {buildtree (node.leftchild, data);//recursively traverse left subtree}} else {  
                if (Node.rightchild = = null) {Node.rightchild = new node (data);  
        else {buildtree (node.rightchild, data);//recursively Traverse right subtree}} }/** * Sequence traversal two-fork tree thread * @param node/public void Inthreading (node node {if (node!= null) {inthreading (node.leftchild);//threaded Zuozi (Recursive) if (Node.leftchil D = = null) {//no left child Node.islefttag = true;//precursor clue node.leftchild = Prenode;//left child refers to Needle pointing precursor} if (Prenode!= null && prenode.rightchild = = NuLL) {//pioneer does not have right child Prenode.isrighttag = true;//successor Clue Prenode.rightchild = node;//Pioneer Right Kid  
            The child pointer points to the successor} Prenode = node; Inthreading (Node.rightchild);  Threaded right subtree (recursive)}}/** * Sequence traversal thread two fork tree * * @param node/public void Inorderthread (node node) {if (node!= null) {while (node!= null &&!node.islefttag) {  
            If the left child is not a clue node = node.leftchild;  
                } do {System.out.print ("" + Node.data + "");  
                if (Node.isrighttag) {//If the right child is a cue node = Node.rightchild;  
                    else {//have right child node = Node.rightchild;  
                    while (node!= null &&!node.islefttag) {node = Node.leftchild; }} while (node!= null);  
        }/** * Test class * @param args */public static void main (string[] args) {  
        Int[] arr = {6, 4, 8, 1, 7, 3, 9, 2, 5};  
        Threadtree threadtree = new Threadtree ();  
        for (int i = 0; i < arr.length i++) {threadtree.buildtree (Threadtree.root, arr[i]);//Build a two-pronged tree Threadtree.inthreading (threadtree.root);//using the middle sequence traversal to thread the two-fork Tree Threadtree.inorderthread (threadtree.root) ;//in-sequence traversal thread two Fork Tree}}

Author: csdn Blog zdp072

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.