12 Storage of the fork tree1.1 Sequential Storage
Use arrays top-down, from left to right, to store node elements on a full binary tree , and the node elements numbered I on a fully binary tree are stored in the component labeled I-1 in an array, and then some methods are used to determine the logical parent-child and sibling relationships of the nodes.
According to the nature of binary tree, complete binary tree and full two tree tree with sequential storage is more appropriate, the sequence number of nodes in the tree can only reflect the logical relationship between nodes, which can save storage space, and can use the subscript value of array elements to determine the location of nodes in binary tree, and the relationship between nodes.
For the general two-fork tree must be stored in the form of a complete binary tree, that is, must add some nonexistent virtual nodes, resulting in wasted space.
Figure 12 Cross-tree sequential storage
The key to sequential storage is the position of the array subscript to determine the node, such as the node starting at 1, then the left child of node i is 2*iand the right child is 2*i+1. The nonexistent node is represented by 0.
Recall some of the properties of the binary tree:
(1) Non-empty binary tree k - layer has a maximum of 2k-1 nodes
(2) A height of two forks with H has a maximum of 2h-1 nodes.
First initialize a tree according to the height of the tree, and some useful methods, the code is as follows:
Public classArraybitree<t> { Privateobject[] data; Private intHeight = 3;//the height of the tree defaults to 3 Private intN//number of nodes. PublicArraybitree () {Data=Newobject[(int) Math.pow (2, height)]; Init (); } /*** Specify depth to initialize a tree *@paramdepth of height tree*/ PublicArraybitree (intheight) { This. Height =height; Data=Newobject[(int) Math.pow (2, height)-1]; } Private voidinit () {System.out.println ("The default is to generate a fully binary tree with a height of 3:"); for(inti=0; i< (int) Math.pow (2, height)-1; i++) {Data[i]= I+1; N++; } print (); } /*** Determine if the node exists *@paramindex root node starting from 1 *@return */ Public BooleanIsexist (intindex) { if(Index > N)return false; returnInteger.valueof (data[index-1].tostring ())! = 0; }}
1.2 Hierarchy Traversal
/*** Hierarchical traversal, using queues is implemented*/ Public voidLevelorder () {Ringbuffer<Integer> queue =NewRingbuffer<integer> (n+1); Queue.put (1);//root node Advanced queue while(Queue.size () >0){ intTMP =Queue.get (); System.out.print (data[tmp-1]+ ""); if(Isexist (2 * tmp)) {//If the left dial hand tree exists, the left subtree is numbered into the stackQueue.put (2 *tmp); } if(Isexist (2 * tmp + 1)) {//If the right subtree is present, the right subtree is numbered into the stack,Queue.put (2 * tmp + 1); } }}
1.3 First Order traversal1.3.1 Recursive Implementation
/*** First Order traversal, recursive implementation recursion *@paramindex root node starting from 1*/ Public voidPreorderrecur (intindex) { if(Isexist (Index)) {//determine if the node exists.System.out.print (data[index-1]+ "");//Accessing the root nodePreorderrecur (2*index);//recursively traverse the left subtreePreorderrecur (2*index + 1);//recursive traversal of right sub-tree }}
1.3.2 Non-recursive implementation
Implementation Method 1:
/*** First-order traversal, non-recursive implementation, with the help of the stack to implement the <p> * root node first into the stack, access to the top nodes of the stack, if the right child of the top of the stack is in the stack, if the left child of the top element of the stack exists then into the stack, so loop until the stack empty*/ Public voidpreorder () {Arraystack<Integer> stack =NewArraystack<integer>(n); Stack.push (1);//root node into the stack while(!Stack.isempty ()) { intTMP = Stack.pop ();//take root nodes and treat each node as a root .System.out.print (data[tmp-1]+ "");//Access root node if(Isexist (2 * tmp + 1)) {//if the right subtree of the root node is present, the right subtree is numbered into the stackStack.push (2 * tmp + 1); } if(Isexist (2 * tmp)) {//if the left subtree of the root node is present, the left subtree is numbered into the stackStack.push (2 *tmp); } }}
Implementation Method 2:
/*** First Order Traversal 1, non-recursive implementation, with the help of stacks to achieve <p> *@paramindex root node starting from 1*/ Public voidPreorderone (intindex) {Arraystack<Integer> stack =NewArraystack<integer>(n); while(isexist (index) | |!Stack.isempty ()) { //(1) First access the root node, go straight to the bottom left, until a left child does not exist in the node. while(isexist (index)) {System.out.print (Data[index-1] + ""); Stack.push (index); //root node into the stack, each node as a root node, to check whether the child exists in the left and rightindex = 2 *index; } //at this point, the stack is the left child from the root node left children, the last node is no left child's node//(2) take the top element of the stack to see if the right child is present, put the current node at its right child, and continue to cycle judgment (1) . if(!Stack.isempty ()) { intTMP = Stack.pop ();//Pop-up left sub-tree nodeindex = 2 * tmp + 1;//see if its right child exists. } }}
1.4 Middle Sequence traversal1.4.1 Recursive Implementation
/** * Middle Sequence traversal , recursive implementation recursion * @param index root node starting from 1 */ public inorderrecur (int index) { Span style= "color: #0000ff;" >if 2*index); // System.out.print (data[index-1]+ ""); // inorderrecur (2*index + 1); // recursively traverse right subtree
1.4.2 Non-recursive implementation
/*** Middle sequence traversal, non-recursive implementation, change access time *@paramIndex*/ Public voidInorder (intindex) {Arraystack<Integer> stack =NewArraystack<integer>(n); while(isexist (index) | |!Stack.isempty ()) { while(isexist (index)) {Stack.push ( index);//root node into the stackindex = 2 * index;//is there a left child } if(!Stack.isempty ()) { intTMP = Stack.pop ();//Eject left childSystem.out.print (data[tmp-1]+ "");//Access Nodesindex = 2 * tmp + 1;//See if the left child's right child exists } }}
1.5 post-post traversal1.5.1 Recursive Implementation
/** * post-sequential traversal , recursive implementation recursion * @param index root node starting from 1 */ public postorderrecur (int index) { if 2*index); // postorderrecur (2*index + 1); // recursively traverse right subtree System.out.print (data[index-1]+ ""); // Access root node
1.5.2 Non-recursive implementation
/*** post-order traversal, non-recursive implementation <p> * Compared to the previous middle sequence implementation is more troublesome, first visit Zuozi and then access the right subtree*/ Public voidPostorder (intindex) {Arraystack<Integer> stack =NewArraystack<integer>(n); intvisited = 0;//mark the previous visited node while(isexist (index) | |!Stack.isempty ()) { while(isexist (index)) {Stack.push ( index);//root node into the stackindex = 2 *index; }//Find all the left children of index first inttop = Stack.peek ();//view stack top element, no popup, after access to the right child after the access to the root node//If the current node does not exist right child or right child has already visited, then access the current node if(!isexist (2*top+1) | | (2*top+1) = =visited) { intTMP =Stack.pop (); System.out.print (data[tmp-1]+ ""); Visited=tmp; } Else{//Otherwise access right childindex = 2 * top + 1; } }}
2 Testing
Public Static voidMain (string[] args) {Arraybitree<Integer> Bitree =NewArraybitree<integer>(); System.out.print ("First order Traversal (recursion):"); Bitree.preorderrecur (1); System.out.print ("\ n sequence Traversal (recursive):"); Bitree.inorderrecur (1); System.out.print ("\ nthe post-traversal (recursion):"); Bitree.postorderrecur (1); System.out.print ("\ n Hierarchy Traversal:"); Bitree.levelorder (); System.out.print ("\ n Sequential traversal (non-recursive):"); Bitree.preorder ();//Bitree.preorderone (1);System.out.print ("\ n-Sequence traversal (non-recursive):"); Bitree.inorder (1); System.out.print ("\ nthe post-traversal (non-recursive):"); Bitree.postorder (1); System.out.println (); Bitree.stdin ();}
2.1 Output Results
Binary tree sequential storage and traversal