Binary Tree JAVA Implementation

Source: Internet
Author: User

 

Package source. datastruct_algorithm;

Import java. util. Stack;

Class baseobj {
Public baseobj (string key ){
This. Key = key;
}
String key;
}
// Node
Class node <t extends baseobj> {
T OBJ;
Node <t> leftnode;
Node <t> rightnode;
Public node (t obj ){
This. OBJ = OBJ;
}
}
// Binary Tree
Public class searchtree <t extends baseobj> {
Private node <t> root;
// Search for a node based on the execution keyword
Public node <t> Find (string key ){
Node <t> cur_node = root;
While (! Key. Equals (cur_node.obj.key )){
If (key. compareto (cur_node.obj.key) <0 ){
Cur_node = cur_node.leftnode;
}
Else {
Cur_node = cur_node.rightnode;
}
If (cur_node = NULL ){
Return NULL;
}
}
Return cur_node;
}
// Insert a node
Public void insert (node <t> newnode ){
If (root = NULL) root = newnode;
Else {
Node <t> cur_node = root;
Node <t> cur_parent;
While (true ){
Cur_parent = cur_node;

If (newnode. obj. Key. compareto (cur_node.obj.key) <0 ){
Cur_node = cur_node.leftnode;
If (cur_node = NULL ){
Cur_parent.leftnode = newnode;
Return;
}
} Else {
Cur_node = cur_node.rightnode;
If (cur_node = NULL ){
Cur_parent.rightnode = newnode;
Return;
}
}
}
}
}
// Sequential Traversal
Public void midorder (node <t> node ){
If (node! = NULL ){
Midorder (node. leftnode );
System. Out. Print (node. obj. Key + "");
Midorder (node. rightnode );
}
}
// Pre-order traversal
Public void preorder (node <t> node ){
If (node! = NULL ){
System. Out. Print (node. obj. Key + "");
Preorder (node. leftnode );
Preorder (node. rightnode );
}
}
// Subsequent Traversal
Public void nextorder (node <t> node ){
If (node! = NULL ){
Nextorder (node. leftnode );
Nextorder (node. rightnode );
System. Out. Print (node. obj. Key + "");
}
}
// Obtain the node with the smallest keyword
Public node <t> getmin (){
Node <t> cur_node, last_node = NULL;
Cur_node = root;
While (cur_node! = NULL ){
Last_node = cur_node;
Cur_node = cur_node.leftnode;
}
Return last_node;
}
// Obtain the node with the largest keyword
Public node <t> getmax (){
Node <t> cur_node, last_node = NULL;
Cur_node = root;
While (cur_node! = NULL ){
Last_node = cur_node;
Cur_node = cur_node.rightnode;
}
Return last_node;
}
// Delete a node based on the keyword
Public Boolean delede (string key ){
Node <t> cur_node, parent_node = NULL;
Boolean isleft = true;
Cur_node = root;
// Find the node need to Del
While (! Key. Equals (cur_node.obj.key )){
Parent_node = cur_node;
If (key. compareto (cur_node.obj.key)> 0 ){
Isleft = true;
Cur_node = cur_node.leftnode;
}
Else {
Isleft = false;
Cur_node = cur_node.rightnode;
}
If (cur_node = NULL ){
Return false;
}
}
// Execute del if don't have childnode
If (cur_node.leftnode = NULL & cur_node.rightnode = NULL ){
If (cur_node = root ){
Root = NULL;
}
Else if (isleft ){
Parent_node.leftnode = NULL;
} Else {
Parent_node.rightnode = NULL;
}
} Else if (cur_node.leftnode = NULL ){
If (cur_node = root ){
Root = cur_node.rightnode;
} Else if (isleft ){
Parent_node.leftnode = cur_node.rightnode;
} Else {
Parent_node.rightnode = cur_node.rightnode;
}
} Else if (cur_node.rightnode = NULL ){
If (cur_node = root ){
Root = cur_node.leftnode;
} Else if (isleft ){
Parent_node.leftnode = cur_node.leftnode;
} Else {
Parent_node.rightnode = cur_node.leftnode;
}
} Else {
Node <t> insteadnode = getinsteadnode (cur_node );
If (cur_node.obj.key.equals (root. obj. Key )){
Root = insteadnode;
}
Else if (isleft ){
Parent_node.leftnode = insteadnode;
} Else {
Parent_node.rightnode = insteadnode;
}
Insteadnode. leftnode = cur_node.leftnode;
}
Return true;

}

// Find the successor node of tech supplement
Private node <t> getinsteadnode (node <t> delnode ){
Node <t> instead_parent = NULL, instead = NULL, cur_node = delnode. rightnode;
While (cur_node! = NULL ){
Instead_parent = instead;
Instead = cur_node;
Cur_node = cur_node.leftnode;
}

If (instead! = Delnode. rightnode ){
Instead_parent.leftnode = instead. rightnode;
Instead. rightnode = delnode. rightnode;
}
Return instead;
}
// Display the entire tree in a tree
Public void display (){
Stack <node <t> globalstack = new stack <node <t> ();
Globalstack. Push (Root );
Int nblanks = 32;
Boolean isempty = false;
While (isempty = false ){
Stack <node <t> localstack = new stack <node <t> ();
Isempty = true;
For (Int J = 0; j <nblanks; j ++ ){
System. Out. println ("");
}
While (globalstack. isempty () = false ){
Node <t> node = globalstack. Pop ();
If (node! = NULL ){
System. Out. Print (node. obj. Key );
Localstack. Push (node. leftnode );
Localstack. Push (node. rightnode );
If (node. leftnode! = NULL | node. rightnode! = NULL ){
Isempty = false;
}
} Else {
System. Out. Print ("-");
Localstack. Push (null );
Localstack. Push (null );
}
For (INT I = 0; I <nblanks * 2 + 2; I ++ ){
System. Out. Print ("");
}
}
System. Out. println ();
Nblanks/= 2;
While (localstack. isempty () = false ){
Globalstack. Push (localstack. Pop ());
}

}

}
Public static void main (string [] ARGs ){
Searchtree <baseobj> tree = new searchtree <baseobj> ();
Node <baseobj> nodeone = new node <baseobj> (New baseobj (""));
Node <baseobj> nodeone1 = new node <baseobj> (New baseobj ("Z "));
Node <baseobj> nodeone2 = new node <baseobj> (New baseobj ("G "));
Node <baseobj> nodeone3 = new node <baseobj> (New baseobj ("F "));
Node <baseobj> nodeone4 = new node <baseobj> (New baseobj ("Q "));
Node <baseobj> nodeone5 = new node <baseobj> (New baseobj ("H "));
Node <baseobj> nodeone6 = new node <baseobj> (New baseobj ("M "));
Node <baseobj> nodeone7 = new node <baseobj> (New baseobj ("B "));
Node <baseobj> nodeone8 = new node <baseobj> (New baseobj ("J "));
Node <baseobj> nodeone9 = new node <baseobj> (New baseobj ("L "));
Node <baseobj> nodeone10 = new node <baseobj> (New baseobj ("C "));

Node <baseobj> nodeone11 = new node <baseobj> (New baseobj (""));
Node <baseobj> nodeone12 = new node <baseobj> (New baseobj ("H "));
Node <baseobj> nodeone13 = new node <baseobj> (New baseobj ("F "));
Node <baseobj> nodeone14 = new node <baseobj> (New baseobj ("U "));
Node <baseobj> nodeone15 = new node <baseobj> (New baseobj ("X "));
Node <baseobj> nodeone16 = new node <baseobj> (New baseobj ("W "));
Node <baseobj> nodeone17 = new node <baseobj> (New baseobj ("S "));
Node <baseobj> nodeone18 = new node <baseobj> (New baseobj ("I "));
Node <baseobj> nodeone19 = new node <baseobj> (New baseobj ("K "));
Tree. insert (nodeone );
Tree. insert (nodeone2 );

Tree. insert (nodeone14 );
Tree. insert (nodeone15 );
Tree. insert (nodeone16 );
Tree. insert (nodeone17 );

Tree. insert (nodeone3 );
Tree. insert (nodeone4 );
Tree. insert (nodeone11 );
Tree. insert (nodeone12 );
Tree. insert (nodeone13 );
Tree. insert (nodeone5 );
Tree. insert (nodeone6 );
Tree. insert (nodeone7 );
Tree. insert (nodeone8 );
Tree. insert (nodeone9 );
Tree. insert (nodeone10 );
Tree. insert (nodeone19 );
Tree. insert (nodeone18 );
Tree. insert (nodeone1 );
Tree. Display ();

Tree. delede ("");
Tree. midorder (tree. Root );

}
}

The binary tree takes into account the efficiency of searching ordered arrays and the efficiency of inserting and deleting arrays in the data structure. Is a good choice. This binary tree is implemented as a non-balanced binary tree. Next, we will continue to achieve the balance of Binary Trees.

Reprinted: http://blog.csdn.net/maomaolingyu/archive/2010/11/18/6018710.aspx

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.