Clue Two fork Tree

Source: Internet
Author: User

1. Basic Concepts

In chained storage, it is found that there are a large number of NULL pointers in the two-linked list, and it is more convenient to use some binary tree operation algorithms If these null pointers are used to point to their direct precursor or subsequent pointers. The clue of the binary tree is to speed up the discovery of the node precursor and the subsequent speed.

In a two-fork tree with n nodes, there is a n+1 null pointer. Each leaf node has 2 null pointers, a node with a degree of 1 has 1 null pointers, the total null pointer is 2n0+n1, and n0=n2+1, so the total null pointer is n0+n1+n2+1=n+1.

Binary tree clue rule: if there is no left subtree, the lchild points to its precursor node, and if there is no right subtree, the rchild points to its successor node. , adding two flag fields to indicate whether the current pointer points to the left or right node or to the predecessor node.

Where the flag bit means:

Clue two the node of the fork tree:

Private class node{    int  data;    Node lchild, rchild;     int ltag = 0, Rtag = 0;}

The two-fork linked list, which is composed of such nodes, is the storage structure of a binary tree, called the clue list , which points to the precursor and the successor, called the Clue, and the two-tree of the clue is called the Clue two fork Tree , The process of traversing a two-fork tree in some order to turn it into a clue two fork tree is called a clue .

2. Construction and clue of two-fork tree

construction : Directly using a complete binary tree sequence to create a two-fork tree , does not exist in the use of 0, here is no longer repeat. Give the code directly.

 Public classThreadtree {PrivateInteger[] nodes;//storing a complete binary tree sequence    Private intN//tree Node pointsNode Root;//root nodeNode Pre;//the node of the previous access    Private classnode{intdata;        Node Lchild, Rchild; intLtag = 0, Rtag = 0; }         PublicThreadtree () {System.out.println ("Enter a complete binary tree sequence, the nonexistent nodes are replaced with 0, separated by commas:");//string[] ins = stdin.readstring (). Split (",");string[] ins = "1,2,3,0,4,0,5". Split (","); Nodes=NewInteger[ins.length];  for(inti = 0; i < ins.length; i++) {Nodes[i]=integer.valueof (Ins[i]); } N=ins.length; Root= Build (1);    Treeutil.print (Depth (root), n, nodes); }        /*** Recursive creation of a binary tree * <p> * Using a complete binary tree sequence*/     PublicNode Build (intindex) {        if(Index >N) {return NULL; }        if(nodes[index-1]==0){            return NULL; } node Node=NewNode (); Node.data= Nodes[index-1]; Node.lchild= Build (2 *index); Node.rchild= Build (2 * index + 1); returnnode; }}

clue a binary tree is actually traversing a binary tree, traversing the process, check the current node left and right whether the pointer is empty, if it is empty, change them to point to the precursor or the successor node.

Taking the middle sequence traversal as an example, the predecessor and subsequent nodes of the nodes are the front and back nodes in the sequential traversal sequence of the binary tree.

algorithm Description : node points to the current node, and the pre points to the previous access node

(1) If the left child of node is empty, modify the left pointer to the pre and set Ltag to 1

(2) If the pre is not empty and there is no right child, modify the right pointer to node and set Rtag to 1

(3) Point the pre to the node you just visited, that is, Pre = node

 Public voidinthreaded () {inthreaded (root); Pre.rchild=NULL;//deal with the last node separately.Pre.rtag = 1;}/*** Middle sequence clue a binary tree*/ Public voidinthreaded (node node) {if(Node! =NULL) {inthreaded (node.lchild);//Cue the left subtree and find the left one without the left child's node .        if(Node.lchild = =NULL){//left child is emptyNode.lchild = pre;//Modify to point to its predecessor nodeNode.ltag = 1;//Modify Flag bit        }        if(Pre! =NULL&& Pre.rchild = =NULL){//If the previous access node is not empty and there is no right childpre.rchild = node;//Modify to point to its subsequent nodePre.rtag = 1;//Modify Flag bit} Pre=node; Inthreaded (Node.rchild); //clues to the right sub-tree    }}
3. Threaded Traversal

The main purpose of the sequential cue binary tree is to speed up the access precursor and the subsequent speed, which does not need to use the stack, because the node implies the information of the precursor and the successor node. Clue without head node two fork Tree traversal algorithm is as follows:

(1) first found in the middle sequence clue two the first node in the tree, left no left child's node, not necessarily a leaf node, according to the Ltag mark as the judge.

(2) Find the successor node of the node, according to Rtag judgment.

/*** Middle sequence clue two cross-tree traversal, non-recursive *@paramnode*/ Public voidinorder (node node) {node TMP=firstnode (node);  while(TMP! =NULL) {System.out.print (Tmp.data+" "); TMP=NextNode (TMP); }}/*** Ask for the first node. *@paramnode *@return */ Publicnode Firstnode (node node) { while(Node.ltag = = 0) {//the leftmost lower node, not necessarily the leaf node .node =Node.lchild; }    returnnode;}/*** to seek subsequent nodes *@paramnode *@return */ Publicnode NextNode (node node) {if(Node.rtag = = 0) {//Presence Right Child        returnFirstnode (Node.rchild);//find the leftmost bottom node at the root of this node.    }    returnNode.rchild;//Rtag = 1 Direct return to successor}/*** Reverse Non-recursive traversal *@paramnode*/ Public voidInordero (node node) {node TMP=lastnode (node);  while(TMP! =NULL) {System.out.print (Tmp.data+" "); TMP=Prenode (TMP); }}/*** To find the last node *@return */ Publicnode Lastnode (node node) { while(Node.rtag = = 0) {node=Node.rchild; }    returnnode;}/*** Find the precursor node *@return */ Publicnode Prenode (node node) {if(Node.ltag = = 0){        returnLastnode (Node.lchild); }    returnNode.lchild;}
4. Testing
 Public Static void Main (string[] args) {    new  threadtree ();    System.out.print ("The total number of nodes of the binary tree:" + Tree.nodes (tree.root));    System.out.print ("\ n sequence traversal recursion:");    Tree.inorderrecur (tree.root);    Tree.inthreaded ();    System.out.print ("n-thread ... \ n-sequence-threaded binary-tree traversal non-recursion:");    Tree.inorder (tree.root);    System.out.print ("\ n non-recursive Traversal (reverse):");    Tree.inordero (tree.root);}
4.1 Output Results

Clue Two fork 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.