Java implementation of two fork tree construction and 3 kinds of traversal methods

Source: Internet
Author: User

Directory:

1. Assign the value of an array to a binary tree

2. Specific code


1. How to build a tree

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> ();
Converts the value of an array to node nodes
for (int nodeindex = 0; Nodeindex < array.length; nodeindex++) {
Nodelist.add (the new node (arr Ay[nodeindex]));
}
//To the first LastParentIndex-1 parent node based on the parent node's number relationship with the child node two 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 not have a right child, take it out alone processing
int lastparentindex = ARRAY.LENGTH/2-1;
Left child
Nodelist.get (lastparentindex). leftchild = NodeList
. Get (Lastparentindex * 2 + 1);
Right child, if the length of the array is odd to establish right child
if (array.length% = = 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 the 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-post traversal:");
Postordertraverse (root);
}

}

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 3 kinds of traversal methods

Related Article

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.