Java implements binary tree and related traversal methods, and java implements Binary Tree

Implement binary tree and related traversal methods in Java

In computer science, a binary tree is a tree structure with a maximum of two Subtrees on each node. Generally, a subtree is called "left subtree" and "right subtree ). Binary Tree is often used to implement binary search tree and binary heap. The following uses Java to implement first-order traversal of Binary Trees, middle-order traversal, post-order traversal, breadth-first traversal, and depth-first traversal. Please note: http://blog.csdn.net/qiuzhping/article/details/44830369

Package com. qiuzhping. tree; import java. util. arrayDeque; import java. util. using list; import java. util. list;/*** function: stores the values of an array in a binary tree and traverses the values in three ways. * constructed binary tree: * 1 */\ * 2 3 */\ * 4 5 6 7 */\ * 8 9 * First traversal: DLR * 1 2 4 8 9 5 3 6 7 * middle-order 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, January 1, April 2015 2] * @ see [Related classes/methods] * @ since [product/module version] */public class binaryTreeTest {private int [] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; private static List <Node> nodeList = null;/*** internal class: node **/private static class Node {Node leftChild; Node rightChild; int data; Node (int newData) {leftChild = null; rightChild = null; data = newData ;}} /** each node of a binary tree has at most two Subtrees (nodes with no degree of presence greater than 2). the Subtrees of a binary tree have left and right points, and the order cannot be Reversed. <BR> * The I layer of the binary tree has at most 2 ^ {I-1} nodes; the depth of k has at most 2 ^ K-1 nodes; <BR> * For any binary tree T, if the number of terminal knots is n_0 and the number of knots whose degree is 2 is n_2, n_0 = n_2 + 1. <BR> * a depth of k, and 2 ^ K-1 nodes called full binary tree; depth of k, there are n nodes of the binary tree, <BR> * if and only when each node is in a full binary tree with a depth of k and the node with a serial number of 1 to n, it is called a Complete Binary Tree. <BR> * @ author Peter. qiu [Parameters description] * @ return void [Return type description] * @ exception throws [Exception] [Exception description] * @ see [Related classes # Related methods # Related properties] */ public void createTree () {nodeList = new inclulist <Node> (); // converts the values of an array to Node for (int nodeI Ndex = 0; nodeIndex <array. length; nodeIndex ++) {nodeList. add (new Node (array [nodeIndex]);} // create a binary tree for (int parentIndex = 0; parentIndex <array. length/2-1; parentIndex ++) {// 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 have no right child Child, so the int lastParentIndex = array. length/2-1; // left child nodeList. get (lastParentIndex ). leftChild = nodeList. get (lastParentIndex * 2 + 1); // right child. if the array length is odd, the 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, only the order is different. ** @ param node * traversal Node */public void preOrderTraverse (node) {if (node = null) Return; System. out. print (node. data + ""); preOrderTraverse (node. leftChild); preOrderTraverse (node. rightChild);}/** central traversal ** these three different traversal structures are the same, only the order is different. ** @ param node * traversal Node */public void inOrderTraverse (node) {if (node = null) return; inOrderTraverse (node. leftChild); System. out. print (node. data + ""); inOrderTraverse (node. rightChild);}/*** post-order traversal ** the three different traversal structures are the same, but the order is different. ** @ param nod E * traversal Node */public void postOrderTraverse (node) {if (node = null) return; postOrderTraverse (Node. leftChild); postOrderTraverse (node. rightChild); System. out. print (node. data + "");}/*** depth-first traversal, equivalent to root traversal * Non-Recursive Implementation * 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 ArrayDeq Ue <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 * Non-Recursive Implementation * secondary data structure required: 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 (nod E. leftChild! = Null) {queue. add (node. leftChild);} if (node. rightChild! = Null) {queue. add (node. rightChild) ;}} System. out. print ("\ n");}/*** constructed binary tree: * 1 */\ * 2 3 */\ * 4 5 6 7 */\ * 8 9 * First traversal: DLR * 1 2 4 8 9 5 3 6 7 * middle-order 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 * /public static void main (String [] args) {binaryTreeTest binTree = new binaryTreeTest (); binTree. createTree (); // The value of The 0th indexes in nodeList is the root Node root = nodeList. get (0); System. out. println ("sequential traversal:"); binTree. preOrderTraverse (root); System. out. println (); System. out. println ("ordinal traversal:"); // LDRbinTree. inOrderTraverse (root); System. out. println (); System. out. println ("sequential traversal:"); // LRDbinTree. postOrderTraverse (root); binTree. depthOrderTraversal (root); // The binTree is traversed in depth. levelOrderTraversal (root); // span traversal }}