Java implementation of two fork tree construction and traversal

Source: Internet
Author: User

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
  1. Package tree;
  2. Import java.util.LinkedList;
  3. Import java.util.List;
  4. /**
  5. * Function: The value of an array is stored in a two-fork tree and then traversed in 3 ways
  6. *
  7. * Reference 0: Data structure (c language version) Min
  8. *
  9. * Reference 1:http://zhidao.baidu.com/question/81938912.html
  10. *
  11. * Reference 2:http://cslibrary.stanford.edu/110/binarytrees.html#java
  12. *
  13. * @author [email protected] @date: 2011-5-17
  14. *
  15. */
  16. Public class BinTreeTraverse2 {
  17. private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
  18. private static list<node> nodeList = null;
  19. /** 
  20. * Inner class: node
  21. *
  22. * @author [email protected] @date: 2011-5-17
  23. *
  24. */
  25. private static class Node {
  26. Node Leftchild;
  27. Node Rightchild;
  28. int data;
  29. Node (int newdata) {
  30. Leftchild = null;
  31. Rightchild = null;
  32. data = NewData;
  33. }
  34. }
  35. public void Createbintree () {
  36. NodeList = new linkedlist<node> ();
  37. //Convert the values of an array to node nodes in turn
  38. For (int nodeindex = 0; nodeindex < array.length; nodeindex++) {
  39. Nodelist.add (new Node (Array[nodeindex));
  40. }
  41. //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
  42. For (int parentindex = 0; parentindex < array.length/ 2- 1; parentindex++) {
  43. //Left child
  44. Nodelist.get (parentindex). Leftchild = NodeList
  45. . Get (Parentindex * 2 + 1);
  46. //Right child
  47. Nodelist.get (parentindex). Rightchild = NodeList
  48. . Get (Parentindex * 2 + 2);
  49. }
  50. ///Last parent node: Because the last parent node may not have a right child, so take it out alone
  51. int lastparentindex = array.length/ 2- 1;
  52. //Left child
  53. Nodelist.get (lastparentindex). Leftchild = NodeList
  54. . Get (Lastparentindex * 2 + 1);
  55. //Right child, if the array length is odd to establish right child
  56. if (array.length% 2 = = 1) {
  57. Nodelist.get (lastparentindex). Rightchild = NodeList
  58. . Get (Lastparentindex * 2 + 2);
  59. }
  60. }
  61. /** 
  62. * First Order traversal
  63. *
  64. * These three different traversal structures are the same, but the order is not the same
  65. *
  66. * @param node
  67. * Traversed nodes
  68. */
  69. public static void Preordertraverse (node node) {
  70. if (node = = null)
  71. return;
  72. System.out.print (Node.data + "");
  73. Preordertraverse (Node.leftchild);
  74. Preordertraverse (Node.rightchild);
  75. }
  76. /** 
  77. * Middle Sequence traversal
  78. *
  79. * These three different traversal structures are the same, but the order is not the same
  80. *
  81. * @param node
  82. * Traversed nodes
  83. */
  84. public static void Inordertraverse (node node) {
  85. if (node = = null)
  86. return;
  87. Inordertraverse (Node.leftchild);
  88. System.out.print (Node.data + "");
  89. Inordertraverse (Node.rightchild);
  90. }
  91. /** 
  92. * Post-sequential traversal
  93. *
  94. * These three different traversal structures are the same, but the order is not the same
  95. *
  96. * @param node
  97. * Traversed nodes
  98. */
  99. public static void Postordertraverse (node node) {
  100. if (node = = null)
  101. return;
  102. Postordertraverse (Node.leftchild);
  103. Postordertraverse (Node.rightchild);
  104. System.out.print (Node.data + "");
  105. }
  106. public static void Main (string[] args) {
  107. BinTreeTraverse2 bintree = new BinTreeTraverse2 ();
  108. Bintree.createbintree ();
  109. the value at No. 0 index in//nodelist is the root node
  110. Node root = Nodelist.get (0);
  111. System.out.println ("First Order traversal:");
  112. Preordertraverse (root);
  113. System.out.println ();
  114. System.out.println ("Middle sequence Traversal:");
  115. Inordertraverse (root);
  116. System.out.println ();
  117. System.out.println ("post-posttraversal:");
  118. Postordertraverse (root);
  119. }
  120. }



Output Result:

Java code
    1. First-order Traversal:
    2. 1 2 4 8 9 5 3 6 7
    3. Middle Sequence Traversal:
    4. 8 4 9 2 5 1 6 3 7
    5. Post-post traversal:
    6. 8 9 4 5 2 6 7 3 1

Java implementation of two fork tree construction and traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.