Reprint: http://ocaicai.iteye.com/blog/1047397
Directory:
1. Assign the value of an array to a binary tree
2. Specific code
1. How to build a tree
2. Specific code
Java code
- Package tree;
- 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
- *
- * Reference 0: Data structure (c language version) Min
- *
- * Reference 1:http://zhidao.baidu.com/question/81938912.html
- *
- * Reference 2:http://cslibrary.stanford.edu/110/binarytrees.html#java
- *
- * @author [email protected] @date: 2011-5-17
- *
- */
- Public class BinTreeTraverse2 {
- private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
- private static list<node> nodeList = null;
- /**
- * Inner class: node
- *
- * @author [email protected] @date: 2011-5-17
- *
- */
- private static class Node {
- Node Leftchild;
- Node Rightchild;
- int data;
- Node (int newdata) {
- Leftchild = null;
- Rightchild = null;
- data = NewData;
- }
- }
- public void Createbintree () {
- NodeList = new linkedlist<node> ();
- //Convert the values of an array to node nodes in turn
- For (int nodeindex = 0; nodeindex < array.length; nodeindex++) {
- Nodelist.add (new Node (Array[nodeindex));
- }
- //To the first LastParentIndex-1 parent node establish a two-fork tree based on the number relationship of the parent node to the child node
- 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 not have a right child, so take it out alone
- 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 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 static void Preordertraverse (node node) {
- if (node = = null)
- return;
- System.out.print (Node.data + "");
- Preordertraverse (Node.leftchild);
- Preordertraverse (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 static void Inordertraverse (node node) {
- if (node = = null)
- return;
- Inordertraverse (Node.leftchild);
- System.out.print (Node.data + "");
- Inordertraverse (Node.rightchild);
- }
- /**
- * Post-sequential traversal
- *
- * These three different traversal structures are the same, but the order is not the same
- *
- * @param node
- * Traversed nodes
- */
- public static void Postordertraverse (node node) {
- if (node = = null)
- return;
- Postordertraverse (Node.leftchild);
- Postordertraverse (Node.rightchild);
- System.out.print (Node.data + "");
- }
- public static void Main (string[] args) {
- BinTreeTraverse2 bintree = new BinTreeTraverse2 ();
- Bintree.createbintree ();
- the value at No. 0 index in//nodelist is the root node
- Node root = Nodelist.get (0);
- System.out.println ("First Order traversal:");
- Preordertraverse (root);
- System.out.println ();
- System.out.println ("Middle sequence Traversal:");
- Inordertraverse (root);
- System.out.println ();
- System.out.println ("post-posttraversal:");
- Postordertraverse (root);
- }
- }
Output Result:
Java code
- First-order Traversal:
- 1 2 4 8 9 5 3 6 7
- Middle Sequence Traversal:
- 8 4 9 2 5 1 6 3 7
- Post-post traversal:
- 8 9 4 5 2 6 7 3 1
Java implementation of two fork tree construction and traversal