Storage Structure Binary Tree

Source: Internet
Author: User

There are two types of storage structures for two-tree: sequential storage structure and chained storage structure. Sequential storage StructureFor a full two-fork tree and a fully binary tree, it is possible to store its data elements in a contiguous set of storage units, as seen in 6-3.

When you use a one-dimensional array to implement sequential storage structures. The nodes numbered I in the two-fork tree are stored in the first component of the array. So according to the nature of 6.7, can get node I parent node, left and right child nodes are stored in, 2i and 2i+1? I/2? Component.Figure 6-3 Sequential storage structure
This storage method is very suitable and efficient for full two-tree and fully binary trees. Because the sequential storage structure of two-tree and full-binary tree is not a waste of space, it can also determine the relationship between nodes very quickly based on the formula. However, for the general two-fork tree, it is necessary to use "virtual node" to fill a binary tree into a completely binary tree to store, or can not determine the junction between the precursor may be related, but this will cause the waste of space. One extreme situation is that, in order to store K nodes, a storage unit needs to be 2k-1. Figure 6-4 illustrates this situation. This is a huge waste of storage space, which is a disadvantage of the sequential storage structure.
Fig. 6-4 Sequential storage structure of a single two-branched tree
Chained storage structurethe design of different node structures can form different chain storage structures.

Each node in a binary tree has two children, and each node can be designed to contain at least 3 domains: The data field, the left child domain, and the right child domain. The data field holds the data element, and the left child field holds a pointer to the left child node. The right child field holds a pointer to the right child's node. Seen by 6-5 (a).

The two-fork tree storage structure obtained by using this node structure is called a binary list. Easy proves that there are n+1 empty links in a two-fork list with n nodes. Fig. 6-5 The chain storage structure of the two fork-tree
To facilitate the finding of the parent node, a pointer field can be added to the node structure above, pointing to the parent node of the node.

Seen by 6-5 (b).

The two-fork tree storage structure obtained by using this node structure is called a three-fork linked list. There are also n+1 empty links in a triple-linked list with n nodes. different storage architectures have different methods for implementing two-tree operations. For example, to find a node of the parent node, in the three-linked list is very easy to implement, in the binary list is required from the root node one by one to find.

In the actual application, we should choose the storage structure according to the main operation of the binary tree.

To find the parent node conveniently. We use the triple-linked list as the storage structure of the binary tree. And in section 6.3, the implementation of the basic operation of the binary tree is also based on the three-fork linked list. Here we first give a definition of a node structure with four domains.

code 6-1 two fork tree storage structure node definitionPublic class Bintreenode implements Node {private Object data;//data fieldprivate Bintreenode parent;//Parental nodeprivate Bintreenode lchild;//left childprivate Bintreenode rchild;//Right childprivate int height;//The height of the subtree with the node as its rootprivate int size;//number of descendants of the node (including node itself)102Public Bintreenode () {this (null);}Public Bintreenode (Object e) {data = e; height = 0; size = 1;parent = Lchild = Rchild = null;}/******node interface Method ******/Public Object GetData () {return data;}Public void SetData (Object obj) {data = obj;}/****** Auxiliary method, infer the current node position situation ******///Infer if there is a FatherPublic Boolean hasparent () {return parent!=null;}//Infer if there is a left childPublic Boolean Haslchild () {return lchild!=null;}//Infer if there is a right childPublic Boolean Hasrchild () {return rchild!=null;}//Infer if it is a leaf node.Public Boolean isleaf () {return!haslchild () &&!hasrchild ();}//Infer if the left child is a nodePublic Boolean Islchild () {return (Hasparent () &&this==parent.lchild);}//Infer if the right child is a node.Public Boolean Isrchild () {return (Hasparent () &&this==parent.rchild);}/****** method of height-related ******/The height of the node, i.e. the height of the tree at which the node is rootedpublic int GetHeight () {return height;}//Update the height of the current node and its ancestorsPublic void Updateheight () {int newh = 0;//new height is initialized to 0, height equals left and right sub-tree height plus 1 largeif (Haslchild ()) NEWH = Math.max (Newh,1+getlchild (). GetHeight ());if (Hasrchild ()) NEWH = Math.max (Newh,1+getrchild (). GetHeight ());if (newh==height) return;//The height does not change and returns directlyHeight = NEWH;//otherwise update altitudeif (hasparent ()) getParent (). Updateheight ();//recursive update ancestor height}/****** methods related to size ******///Take the node of the tree with the node rootpublic int GetSize () {return size;}//Update current node and descendants of ancestorsPublic void Updatesize () {size = 1;//initialized to 1, node itselfif (Haslchild ()) Size + = Getlchild (). GetSize ();//plus left subtree sizeif (Hasrchild ()) Size + = Getrchild (). GetSize ();//Plus right subtree sizeif (hasparent ()) getParent (). Updatesize ();//recursively update the size of ancestors}/****** methods associated with the parent ******///Take parent nodePublic Bintreenode GetParent () {return parent;}//Disconnect from father's relationshipPublic void Sever () {if (!hasparent ()) return;if (Islchild ()) parent.lchild = null;else parent.rchild = null;parent.updateheight ();//Update parent node and its ancestor heightparent.updatesize ();//Update parent node and its ancestor sizeparent = null;}/****** methods related to Lchild ******///Take left childPublic Bintreenode Getlchild () {return lchild;}//Set the left child of the current node, return to the original left childPublic Bintreenode Setlchild (bintreenode LC) {Bintreenode oldlc = this.lchild;if (Haslchild ()) {lchild.sever ();}//Break the current left child's relationship to the nodeif (lc!=null) {lc.sever ();//Disconnect the LC from its parent nodethis.lchild = LC;//Determine parent-child relationshiplc.parent = this;this.updateheight ();//Update the current node and its ancestor heightthis.updatesize ();//Update the current node and its ancestor size}return OLDLC;//back to original left child}/****** methods related to Rchild ******///Take right childPublic Bintreenode Getrchild () {return rchild;}//Set current node right child, return to original right childPublic Bintreenode setrchild (Bintreenode RC) {Bintreenode OLDRC = this.rchild;if (Hasrchild ()) {rchild.sever ();}//Break the current right child's relationship to the nodeif (rc!=null) {rc.sever ();//Disconnect the LC from its parent nodethis.rchild = RC;//Determine parent-child relationship104rc.parent = this;this.updateheight ();//Update the current node and its ancestor heightthis.updatesize ();//Update the current node and its ancestor size}return OLDRC;//back to the original right child}}Code 6-1 Description: The helper method that infers the current node position in the code and the simple Get method are all completed within the constant time. Implementation also corresponds very easy. The following focuses on the implementation and time complexity of updateheight (), Updatesize (), Sever (), Setlchild (LC), Getrchild (RC). ⑴updateheight (): If the current node v child changes, you need to use the Updateheight () method to update the height of the current node and its ancestor nodes. Please note that. As the height of a node changes, it affects the height of its ancestor nodes, where we agree to run the operation directly on whatever node.

Since the height of a node in a binary tree is equal to the height of its left and right sub-tree, and the height of the left and right sub-tree is greater than the height of the child, it is only necessary to obtain the heights of the children, and only the θ (1) time is required. Continue from V to the parent reference retrograde upward, update the height of each ancestor node in turn.

Assumed in the above process. found that the height of a node has not changed, the algorithm can be directly terminated. Sum up. When the Updateheight () method is called on a node V, if the layer of V is level (v), it is only necessary to update the height of the level (v) +1 nodes. Thus the time-to-complexity of the algorithm is t (n) =0 (level (v)). ⑵updatesize (): A child with the same assumption that node v changes, should update the current node and the size of its ancestors.

Because in a binary tree no matter what the size of a node. is equal to the size of its left and right sub-tree plus the node itself, and the size of the left and right sub-tree only need to get the node around the size of the child can be obtained, only need θ (1) time.

Thus the algorithm's time complexity T (n) =0 (level (v)). ⑶sever (): Cut off the relationship between node V and parent node p.

The algorithm needs to change the pointer field of V and P, which requires constant time. In addition, since the children of P-junction have changed, it is necessary to call Updateheight () and Updatesize () to update the height and scale of the parent node p and its ancestors. Its time complexity t (n) =0 (level (v)). ⑷setlchild (LC), Getrchild (RC): Two algorithms function relative, one is set node V left child. One is the right child to set node v.

The implementation of the two algorithms is similar, as illustrated by the example of Setlchild (). First, suppose V has left child oldlc. You should call OLDLC. Sever () disconnects v from its left child.

Second, the LC is called. Sever () disconnects its relationship to the parent node.

Finally, establish a parent-child relationship between V and LC and Invoke v. Updatesize () and v.updateheight () Update V and its ancestors in size and height. a lot of other exciting content please follow: http://bbs.superwu.cn focus on the two-dimensional code of Superman Academy: Follow the Superman college Java Free Learning Exchange Group:

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Storage Structure 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.