Implementation of binary tree storage structure in Java

Source: Internet
Author: User
Tags pow

One or two fork tree

A binary tree refers to an ordered tree with a maximum of two subtrees per node. Usually the left subtree is called the "left subtree", and the right subtree is called the right sub-tree.

Each node of a binary tree has a maximum of 2 subtrees trees, and the subtree order of the binary tree cannot be reversed.

Second, the realization of sequential storage binary tree
1  PackageCom.ietree.basic.datastructure.tree.binarytree;2 3 /**4 * Created by Ietree5 * 2017/5/16  */7  Public classArraybintree<t> {8 9     //use an array to record all nodes of the treeTen     Privateobject[] datas; One     Private intDefault_deep = 8; A     //Save the depth of the tree -     Private intDeep ; -     Private intarraySize; the  -     //create a two-fork tree with the default depth -      PublicArraybintree () { -          This. Deep =Default_deep; +          This. ArraySize = (int) (Math.pow (2, deep)-1); -Datas =NewObject[arraysize]; +     } A  at     //create a two-fork tree at a specified depth -      PublicArraybintree (intDeep ) { -          This. Deep =Deep ; -          This. ArraySize = (int) Math.pow (2, deep)-1; -Datas =NewObject[arraysize]; -     } in  -     //creates a two-fork tree with the specified depth and specified nodes to      PublicArraybintree (intDeep , T data) { +          This. Deep =Deep ; -          This. ArraySize = (int) Math.pow (2, deep)-1; theDatas =NewObject[arraysize]; *Datas[0] =data; $     }Panax Notoginseng  -     /** the * Add child nodes for the specified node +      * A      * @paramindex the parent node to which the child node needs to be added the      * @paramdata for the new child node +      * @paramleft node -      */ $      Public voidAddintIndex, T data,BooleanLeft ) { $         if(Datas[index] = =NULL) { -             Throw NewRuntimeException (Index + "node is empty, cannot add child nodes"); -         } the         if(2 * index + 1 >=arraySize) { -             Throw NewRuntimeException ("The array at the bottom of the tree is full, the tree is out of bounds");Wuyi         } the         //Add left child node -         if(left) { WuDATAS[2 * index + 1] =data; -}Else { AboutDATAS[2 * index + 2] =data; $         } -     } -  -     //determine if the binary tree is empty A      Public Booleanempty () { +         //determine if the binary tree is empty based on the root element the         returnDatas[0] = =NULL; -     } $  the     //returns the root node the      PublicT root () { the         return(T) Datas[0]; the     } -  in     //returns the parent node of the specified node (not the root node) the      PublicT Parent (intindex) { the         return(T) datas[(index-1)/2]; About     } the  the     //returns the left child node of the specified node (not a leaf), returning NULL when the left dial hand node does not exist the      PublicT Left (intindex) { +         if(2 * index + 1 >=arraySize) { -             Throw NewRuntimeException ("The node is a leaf node, No child nodes"); the         }Bayi         return(T) Datas[index * 2 + 1]; the     } the  -     //returns the right child node of the specified node (not a leaf), returning null if the right child node does not exist -      PublicT Right (intindex) { the         if(2 * index + 1 >=arraySize) { the             Throw NewRuntimeException ("The node is a leaf node, No child nodes"); the         } the         return(T) Datas[index * 2 + 2]; -     } the  the     //returns the depth of the two fork tree the      Public intDeepintindex) {94         returnDeep ; the     } the  the     //returns the location of the specified node98      Public intPOS (T data) { About         //the loop is actually searching for each node by breadth traversal -          for(inti = 0; i < arraySize; i++) {101             if(datas[i].equals (data)) {102                 returni;103             }104  the         }106         return-1;107     }108 109      PublicString toString () { the         returnjava.util.Arrays.toString (datas);111     } the 113}

Test class:

1  PackageCom.ietree.basic.datastructure.tree.binarytree;2 3 /**4 * Created by Ietree5 * 2017/5/16  */7  Public classArraybintreetest {8 9      Public Static voidMain (string[] args) {Ten  OneArraybintree<string> Bintree =NewArraybintree<string> (4, "root"); ABintree.add (0, "second right child node",false); -Bintree.add (2, "right sub-node of the third layer",false); -Bintree.add (6, "fourth right sub-node",false); the System.out.println (bintree); -          -     } -  +}

Program output:

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL null, layer fourth right child node]
Three or two fork tree two-linked list storage

Four or two fork-tree with three-fork list storage

Implementation of binary tree storage structure in Java

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.