Java implementation Two fork tree and related traversal method
in Computer science, a binary tree is a tree structure that has a maximum of two subtrees per node. The subtree is often referred to as the left subtree and the right subtree (subtree). Binary trees are often used to implement a two-fork search tree and a two-fork heap. The following Java implementation of the two-fork tree sequence traversal, the middle sequence traversal, post-order traversal, breadth-first traversal, depth-first traversal. Please specify: http://blog.csdn.net/qiuzhping/article/details/44830369
Package Com.qiuzhping.tree;import Java.util.arraydeque;import Java.util.linkedlist;import java.util.List;/** * Function: The value of an array is stored in a two-fork tree and then traversed in 3 ways. * Constructed two-fork tree: * 1 */* 2 3 */\/* 4 5 6 7 * /* 8 9 * Sequential traversal: DLR * 1 2 4 8 9 5 3 6 7 * Sequence traversal: LDR * 8 4 2 9 5 1 6 3 7 * post-order traversal: LRD * 8 9 4 5 2 6 7 3 1 * Depth-First traversal * 1 2 4 8 9 5 3 6 7 * Breadth-First traversal * 1 2 3 4 5 6 7 8 9 * @author Peter.qiu * @version [version NO, April 2, 2015] * @see [Related classes/methods] * @since [Product/module version] */public class Binarytreetest {private int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};p rivate static list<node> nodeList = null;/** * Inner class: node * */private static class node {Node leftchild; Node rightchild;int data; Node (int newdata) {leftchild = Null;rightchild = Null;data = NewData;}} /** two fork tree each node at most only two subtrees tree (no more than 2 nodes), the subtree of the binary tree has left and right points, the order can not be reversed. <BR> * The first layer of the binary tree has a maximum of 2^{i-1} nodes; a two-tree with a depth of K has a maximum of 2^k-1 nodes;<br> * to any binary tree T, if its terminal nodeThe number is n_0, and the node with a degree of 2 is n_2, then n_0=n_2+1. <BR> * A depth of k, and there are 2^k-1 nodes called full two-tree, the depth of k, there are n nodes of the two-tree,<br> * When and only if each of its nodes with a depth of K of the full two-fork tree, the sequence number of 1 to n corresponds to the node, called a complete binary tree. <BR> * @author Peter.qiu [Parameters description] * @return void [return type description] * @exception throws [Exc Eption] [Exception description] * @see [related classes#related methods#related properties] */public void Createtree () {No Delist = new linkedlist<node> ();//Convert the value of an array to a node node for (int nodeindex = 0; Nodeindex < array.length; nodeindex+ +) {Nodelist.add (new Node (Array[nodeindex));} Establish a two-tree for (int parentindex = 0; Parentindex < ARRAY.LENGTH/2-1; Parentindex to the first lastParentIndex-1 parent node according to the number relationship of the parent node to the child node + +) {//left child Nodelist.get (parentindex). Leftchild = Nodelist.get (Parentindex * 2 + 1);//Right Child Nodelist.get (Parentindex). Rightchild = Nodelist.get (Parentindex * 2 + 2);} Last parent node: Because the last parent node may not have a right child, take it out alone to handle int lastparentindex = ARRAY.LENGTH/2-1;//left child nodelist.get (Lastparentindex). Leftchild = Nodelist.get (Lastparentindex * 2+ 1);//Right child, if the array length is an odd number to establish right child if (array.length% 2 = = 1) {nodelist.get (lastparentindex). Rightchild = Nodelist.get ( Lastparentindex * 2 + 2);}} /** * First ORDER traversal * * These three different traversal structures are the same, but the order is not the same * * * @param node * traversed nodes */public void Preordertraverse (node node) {if (node = = null) return; System.out.print (Node.data + "");p Reordertraverse (node.leftchild);p reordertraverse (node.rightchild);} /** * Middle Sequence traversal * * These three different traversal structures are the same, but the order is not the same * * * @param node * traversed nodes */public void Inordertraverse (node node) {if (node = = null) return;inordertraverse (node.leftchild); System.out.print (Node.data + ""); Inordertraverse (node.rightchild);} /** * post-order traversal * * These three different traversal structures are the same, but the sequence is not the same * * @param node * traversed nodes */public void Postordertraverse (Node node {if (node = = null) return;postordertraverse (node.leftchild);p ostordertraverse (node.rightchild); System.out.print (Node.data + "");} /** * Depth-first traversal, equivalent to the first root traversal * takes a non-recursive implementation * requires a secondary data structure: Stack */public void depthordertraversal (Node root) { System.out.println ("\ n Depth-first traversal"); if (root==null) {System.out.println ("empty Tree"); Return } arraydeque<node> stack=new arraydeque<node> (); Stack.push (root); while (Stack.isempty () ==false) {Node node=stack.pop (); System.out.print (node.data+ ""); if (node.rightchild!=null) {Stack.push (node.rightchild); } if (Node.leftchild!=null) {Stack.push (node.leftchild); }} System.out.print ("\ n"); }/** * Breadth-First traversal * takes a non-recursive implementation * requires a secondary data structure: Queue */public void levelordertraversal (Node root) {System. Out.println ("Breadth-first traversal"); if (root==null) {System.out.println ("empty Tree"); Return } arraydeque<node> queue=new arraydeque<node> (); Queue.add (root); while (Queue.isempty () ==false) {Node node=queue.remove (); System.out.print (node.data+ ""); if (node.leftchild!=null) {queue.add (node.leftchild); } if (Node.rightchild!=null) {queue.add (node.rightchild); }} System.out.print ("\ n"); }/** * Constructed two-fork tree: * 1 */* 2 3 */\/* 4 5 6 7 * /* 8 9 * Sequential traversal: DLR * 2 4 8 9 5 3 6 7 * Sequence traversal: LDR * 8 4 2 9 5 1 6 3 7 * post-order traversal: LRD * 8 9 4 5 2 6 7 3 1 * Depth-First traversal * 2 4 8 9 5 3 6 7 * Breadth-First traversal * 2 3 4 5 6 7 8 9 */public static void Main (string[] args) {binarytreetes T bintree = new binarytreetest (); Bintree.createtree ();//The value at the No. 0 index in the nodeList is the root node, nodes root = nodelist.get (0); System.out.println ("First Order Traversal:"); Bintree.preordertraverse (root); System.out.println (); System.out.println ("Middle sequence Traversal:");//ldrbintree.inordertraverse (root); System.out.println (); System.out.println ("post-post traversal:");//lrdbintree.postordertraverse (root); Bintree.depthordertraversal (Root);//Depth traversal bintree.levelordertraversal (root);//Breadth Traversal}}
Java implementation Two fork tree and related traversal method