Javascript implements data structure: clue Binary Tree

Source: Internet
Author: User

Javascript implements data structure: clue Binary Tree
Traversing a binary tree is to arrange the nodes in the tree into a linear sequence according to certain rules, that is, to linearly operate the non-linear structure. How can we find the direct precursor and direct successor of each node dynamically obtained during the traversal process (except for the first and last nodes )? How do I save this information? If a binary tree has n nodes, there are n-1 edges (pointer lines). n nodes have 2n pointer fields (Lchild and Rchild ), obviously, n + 1 idle pointer field is not used. These idle pointer fields can be used to store the node's direct precursor and direct successor information. Specify the pointer field of a node as follows: 1. If the node has a left subtree, The leftChild field indicates its left child; otherwise, the leftChild field indicates its precursor. 2. If a node has a right subtree, its rightChild domain indicates its right child; otherwise, its rightChild domain indicates its successor. To avoid confusion, you still need to change the node Structure and add two sign fields (leftTag and rightTag: -- 0 leftChild indicates the left child leftTag of the node -- 1 rightChild indicates the precursor of the node -- 0 rightChild indicates the right child rightTag of the node -- 1 rightChild indicates the successor of the node to this the binary linked list composed of the node structure is used as the storage structure of the binary tree, it is called a chain table, which points to the frontend and the next pointer of a node, and is called a clue. The Binary Tree with clues is called the Threaded Binary Tree ). The process of traversing a binary tree in a certain order to turn it into a clue binary tree is called a clue. Note: When a clue binary tree is drawn, the solid line indicates a pointer pointing to its left and right children; the dotted line indicates a clue pointing to its direct predecessor or direct successor. To traverse the tree, you only need to find the first node in the sequence, and then you can find the direct successor node of the node in sequence until the successor is empty. How to find the direct successor of a node in the clue tree? For example, the middle-order clue tree shown in figure (d) and (e): ◆ the right chain of all leaf nodes in the tree is a clue. The right chain directly indicates the direct successor of the node. For example, the direct successor of node G is node E. ◆ The right link of all non-leaf nodes in the tree is a pointer. Based on the law of sequential traversal, the direct successor of a non-leaf node is the first node accessed when traversing its right subtree, that is, the leftmost (leaf) node in the right subtree. For example, the direct successor of node C: Find the root node F of the right subtree along the right pointer, and then go down along the left chain until the node with Ltag = 1 is the direct successor node H of node C. How to find the direct precursor of a node in the clue tree? If the node's Ltag is 1, the left chain is the clue, indicating that it is the direct precursor; otherwise, the last node accessed when traversing the left subtree (that is, the node with the rightmost down along the left subtree) for its direct precursor node. The direct successor of finding a node in the clue tree that traverses in the descending order is complex and can be divided into three situations: ◆ if the node is the root node of a binary tree, the direct successor is empty; ◆ if the node is the Left or Right child of its parent node and its parent node does not have the right subtree: directly successor to its parent node; ◆ if the node is the left child of its parent node and its parent node has the right subtree: The first node that traverses the right subtree of its parent node in the descending order. The process of turning a binary tree into a binary tree is based on a certain traversal order. The clue-based process is the process of modifying the NULL pointer in the traversal process to direct it to the forward or forward. Like the storage structure of a linear table, a head node is added to the chain table of the binary tree. The pointer field of the header node is arranged as follows: ◆ Lchild field: pointing to the root node of the binary tree; ◆ Rchild domain: points to the last node in the middle sequence. ◆ the Lchild pointer domain of the first node in the binary tree sequence and the Rchild pointer field of the last node point to the head node. For example, a two-way chain table is created for the same binary tree. For a single binary tree, you can start from the original node or start from the last node and traverse directly by searching. Obviously, this traversal does not require a stack. The traversal of a clue binary tree is in a clue binary tree. Because there are clues, in some cases, you can easily find the direct precursor or direct successor of a specified node in a specific traversal sequence. In addition, it is much easier to traverse a clue binary tree than to traverse a general binary tree. You do not need to set a stack and the algorithm is very concise. Code Implementation of the clue Binary Tree: Copy code 1 var LINK = 0; 2 var THREAD = 1; 3 4 function BinaryThreadTree_inOrder (data, leftChild, rightChild) {5 this. data = data; 6 this. leftChild = leftChild | null; 7 this. rightChild = rightChild | null; 8 // mark 9 this. leftTag = this. rightTag = undefined; 10} 11 BinaryThreadTree_inOrder.prototype = {12 constructor: BinaryThreadTree_inOrder, 13 // traverse 14 inOrderTraverse_t Hread: function (visit) {15 var p = this. leftChild; 16 17 while (p! = This) {18 while (p. leftTag = LINK) p = p. leftChild; 19 20 if (visit (p. data) = false) return; 21 22 while (p. rightTag = THREAD & p. rightChild! = This) {23 p = p. rightChild; 24 visit (p. data); 25} 26 p = p. rightChild; 27} 28}, 29 // sequencing 30 inOrderThreading: function () {31 return inOrderThreading (this); 32 }, 33 // insert subtree x into the current node. p represents the current node 34 insertSubTree: function (xTree) {35 var s, q; 36 // x is the left subtree of p 37 if (this. leftTag = THREAD) {38 s = this. leftChild; // s is the precursor of p 39 this. leftTag = LINK; 40 this. leftChild = xTree; 41 q = xTree; 42 43 While (q. leftChild & q. leftTag = LINK) q = q. leftChild; 44 // locate the leftmost node in the subtree and modify its precursor to s 45 q. leftChild = s; 46 xTree. rightTag = THREAD; 47 // the successor of x points to p 48 xTree. rightChild = this; 49} 50 // x is the right subtree of p 51 else if (this. rightTag = THREAD) {52 // s is the next 53 s of p = this. rightChild; 54 this. rightTag = LINK; 55 this. rightChild = xTree; 56 q = xTree; 57 58 while (q. leftChild & q. leftTag = LINK) q = q. leftCh Ild; 59 // locate the leftmost node in the subtree and modify its precursor to p 60 q. leftChild = this; 61 xTree. rightTag = THREAD; 62 // the successor of x points to the successor 63 xTree of p. rightChild = s; 64} 65 // x is the left subtree of p, and the left subtree of p is the right subtree of x 66 else {67 s = this. leftChild; 68 var t = s; 69 70 while (t. leftChild & t. leftTag = LINK) t = t. leftChild; 71 // locate the leftmost node t of the Left subtree of p and the precursor u 72 var u = t. leftChild; 73 this. leftChild = xTree; 74 xTree. rightTag = LINK; 75 // x is the left subtree of p, and the left subtree of p is the right subtree of x. 7 6. xTree. rightChild = s; 77 t. leftChild = xTree; 78 q = xTree; 79 80 while (q. leftChild & q. leftTag = LINK) q = q. leftChild; 81 // locate the leftmost node in the subtree and modify its precursor to u 82 q. leftChild = u; 83} 84} 85}; 86 87 // tree sequencing 88 function inOrderThreading (tree) {89 var threadTree = new BinaryThreadTree (); 90 threadTree. leftTag = LINK; 91 threadTree. rightTag = THREAD; 92 // The right pointer indicates 93 threadTree. rightChild = threadTre E; 94 95 var pre; 96 // if the binary tree is null, the left pointer refers to 97 if (! Tree) threadTree. leftChild = threadTree; 98 else {99 threadTree. leftChild = tree; 100 pre = threadTree; 101 inThreading. rightChild = threadTree; 104 pre. rightTag = threads; 105 threadTree. rightChild = pre; 106} 107 108 return threadTree; 109 110 function inThreading (p) {111 if (! P) return; 112 113 inThreading (p. leftChild); // The left subtree improves the clue by 114 // The precursor clue is 115 if (! P. leftChild) {116 p. leftTag = THREAD; 117 p. leftChild = pre; 118} 119 // 120 if (! Pre. rightChild) {121 pre. rightTag = threads; 122 pre. rightChild = p; 123} 124 pre = p; 125 inThreading (p. rightChild); // right subtree clue 126} 127} copy code

Related Article

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.