Java creates a binary tree to store the numbers in the array into the binary tree in sequence, and uses the first, last, and middle order for traversal respectively;

Source: Internet
Author: User

 

Create a binary tree using Java to store the number in the array into a binary tree in sequence. If the value is negative or zero, the corresponding node is empty.

 

Define the structure of a binary tree

Public class bitree {
Int data;
Bitree left;
Bitree right;
Int I; // the location where the value of this node is stored in the array;
Public bitree (){

}


Public bitree (int I ){
This. I = I;
}
Public bitree (INT data, bitree L, bitree R ){
This. Data = data;
This. Left = L;
This. Right = R;
}
Public void print (){
System. Out. Print (this. Data + ",");
}
}

 

 

Interface

Public interface visittreeinterface {
Public bitree creatroot (bitree node); // create a root node
Public bitree creatleft (bitree node); // Insert the left Node
Public bitree creatright (bitree node); // Insert the right Node
Public void preorder (bitree node); // traverses Binary Trees in ascending order
Public void lastorder (bitree node); // post-order traversal
Public void midorder (bitree node); // supports sequential traversal.

}

Classes that implement interfaces

Public class creattree implements visittreeinterface {

Int [] B;

Public creattree (INT [] ){
This. B =;
 
}

Public bitree creatroot (bitree node) {// create a root node
If (node. I <B. Length ){
If (B [node. I] <= 0)
Node = NULL;
Else if (node! = NULL ){
Node. Data = B [node. I];
System. Out. println (node. I + "" + node. data );
Node. Left = creatleft (node );
Node. Right = creatright (node );

}
}
Return node;
 
}

Public bitree creatleft (bitree node) {// left Node
If (node! = NULL)
{
Node. Left = new bitree ();
Node. Left. I = 2 * node. I + 1; // if the position of the root node in the array is I, the left node is 2 * I + 1

Creatroot (node. Left); // call recursion;
}
Return node. Left;
}

Public bitree creatright (bitree node) {// right Node
If (node! = NULL)
{
Node. Right = new bitree ();
Node. Right. I = 2 * node. I + 2;
Creatroot (node. Right );
}
Return node. Right;
}

Public void preorder (bitree node ){
If (node! = NULL ){
Node. Print ();
Preorder (node. Left );
Preorder (node. Right );
}
}

Public void lastorder (bitree node ){
If (node! = NULL ){
Lastorder (node. Left );
Lastorder (node. Right );
Node. Print ();

}
 
}

Public void midorder (bitree node ){
If (node! = NULL ){
Midorder (node. Left );
Node. Print ();
Midorder (node. Right );
}

}
}

 

Create a binary tree and traverse it

Public class main {
Public static void main (string [] ARGs ){
Int [] B = {,-6, 5, 9, 10 };
Bitree node = new bitree (0 );
Bitree Bt;
Creattree Ct = new creattree (B );
Bt = CT. creatroot (node );
Ct. preorder (BT );
System. Out. Print ("/N ");
Ct. lastorder (BT );
System. Out. Print ("/N ");
Ct. midorder (BT );
System. Out. Print ("/N ");

}

}

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.