Java implementation Two fork tree and related traversal method
in Computer science. A binary tree is a tree structure with 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 is a Java implementation of the two-fork tree of the first 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 each node of the tree has at most two subtrees trees (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-fork tree with a depth of K has at most 2^k-1 nodes;<br> * For whatever a binary tree T, it is assumed that its terminal node number is N_0, and the number of nodes with a degree of 2 is n_2. The 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 two in the tree, the sequence number is 1 to n corresponding to the node. Called the total binary tree .<br> * @author Peter.qiu [Parameters description] * @return void [return type description] * @exception th rows [Exception] [Exception description] * @see [related classes#related methods#related properties] */public void Createt Ree () {nodeList = new linkedlist<node> ();//Convert the value of an array to a node node for (int nodeindex = 0; nodeindex < Array.Length; n odeindex++) {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 alone to handle int lastparentindex = ARRAY.LENGTH/2-1;//left child nodelist.get (lastparentindex). Leftchild = Nodelist.get (Lastparentindex * 2 + 1);//Right child, suppose the array length is odd to establish right child if ( Array.Length% 2 = = 1) {nodelist.get (lastparentindex). Rightchild = Nodelist.get (Lastparentindex * 2 + 2);}} /** * Sequential Traversal * * These three different traversal structures are the same, just 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, just the order is not the same * * @param node * Traversal of nodes */public void Inordertraverse (node node) {if (node = = null) return;inordertraverse (node.leftchild); System.out.print (Node.data + ""); Inordertraverse (node.rightchild);} /** * post-post traversal * * These three different traversal structures are the same. It's just a different order. * * @param node * traversed nodes */public void Postordertraverse (node node) {if (node = = null) return;post Ordertraverse (Node.leftchild);p ostordertraverse (node.rightchild); System.out.print (Node.data + "");} /** * Depth-first traversal, equivalent to root-overCalendar * Using non-recursive implementation * requires auxiliary 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 * adopted non-recursive implementation * requires 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);// Deep traverse bintree.levelordertraversal (root);//Breadth Traversal}}
Java implementation Two fork tree and related traversal method