Binary Tree writing Algorithm in Java

Source: Internet
Author: User

Class Node
{
Int idata; // data used as key value
Double dData; // other data
Node leftchild; // This node's left child
Node rightchild; // This node's right child

Public void displaynode ()
{
// (See Listing 8.1 for method body)
System. Out. Print ("{" + idata + "," + dData + "}");
}
}

Class tree
{
Private node root; // The only data field in tree

Public node find (INT key) // find node with given key
{// (Assumes non-empty tree)
Node current = root; // start at root
While (current. idata! = Key) // while no match,
{
If (Key <Current. idata) // go left?
Current = current. leftchild;
Else
Current = current. rightchild; // or go right?
If (current = NULL) // if no child,
Return NULL; // didn't find it
}
Return Current; // found it
}

Public node minimum () // returns node with minimum key value
{
Node current, last = NULL;
Current = root; // start at root
While (current! = NULL) // until the bottom,
{
Last = current; // remember Node
Current = current. leftchild; // go to left child
}
Return last;
}

Public void insert (int id, double dd)
{
Node newnode = new node (); // make new node
Newnode. idata = ID; // insert data
Newnode. dData = dd;
If (root = NULL) // No node in Root
Root = newnode;
Else // Root occupied
{
Node current = root; // start at root
Node parent;
While (true) // (exits internally)
{
Parent = current;
If (ID <Current. idata) // go left?
{
Current = current. leftchild;
If (current = NULL) // If End of the line,
{// Insert on left
Parent. leftchild = newnode;
Return;
}
} // End if go left
Else // or go right?
{
Current = current. rightchild;
If (current = NULL) // If end of the line
{// Insert on right
Parent. rightchild = newnode;
Return;
}
} // End else go right
} // End while
} // End else not root
} // End insert ()

Public Boolean Delete (INT key) // delete node with given key
{// (Assumes non-empty list)
Node current = root;
Node parent = root;
Boolean isleftchild = true;
While (current. idata! = Key) // search for Node
{
Parent = current;
If (Key <Current. idata) // go left?
{
Isleftchild = true;
Current = current. leftchild;
}
Else // or go right?
{
Isleftchild = false;
Current = current. rightchild;
}
If (current = NULL) // End of the line,
Return false; // didn't find it
} // End while
// Found node to delete
// Continues...
 
// Delete () continued...
// If no children, simply delete it
If (current. leftchild = NULL & Current. rightchild = NULL)
{
If (current = root) // If root,
Root = NULL; // tree is empty
Else if (isleftchild)
Parent. leftchild = NULL; // disconnect
Else // from parent
Parent. rightchild = NULL;
}
// Continues...

// Delete () continued...
// If no right child, replace with left subtree
Else if (current. rightchild = NULL)
If (current = root)
Root = current. leftchild;
Else if (isleftchild) // left child of parent
Parent. leftchild = current. leftchild;
Else // right child of parent
Parent. rightchild = current. leftchild;
// If no left child, replace with right subtree
Else if (current. leftchild = NULL)
If (current = root)
Root = current. rightchild;
Else if (isleftchild) // left child of parent
Parent. leftchild = current. rightchild;
Else // right child of parent
Parent. rightchild = current. rightchild;
// Continued...

// Delete () continued
Else // two children, so replace with inorder successor
{
// Get successor of node to delete (current)
Node successor = getsuccessor (current );
// Connect parent of current to successor instead
If (current = root)
Root = successor;
Else if (isleftchild)
Parent. leftchild = successor;
Else
Parent. rightchild = successor;
// Connect successor to current's left child
Successor. leftchild = current. leftchild;
} // End else two children
// (Successor cannot have a left child)
Return true;
} // End Delete ()
 
Public void printtree (){
System. Out. Print ("Forward traversal :");
Preorder (Root );
System. Out. println ();
System. Out. Print ("sequential traversal :");
Inorder (Root );
System. Out. println ();
System. Out. Print ("sequential traversal :");
Postorder (Root );
System. Out. println ();
}

// Returns node with next-highest value after delnode
// Goes to right child, then right child's left descendants
Private node getsuccessor (node delnode)
{
Node successorparent = delnode;
Node successor = delnode;
Node current = delnode. rightchild; // go to right child
While (current! = NULL) // until no more
{// Left children,
Successorparent = successor;
Successor = current;
Current = current. leftchild; // go to left child
}
// If successor not
If (successor! = Delnode. rightchild) // right child,
{// Make connections
Successorparent. leftchild = successor. rightchild;
Successor. rightchild = delnode. rightchild ;//?
}
Return successor;
}

Private void preorder (node localroot)
{
If (localroot! = NULL)
{
Localroot. displaynode ();
Preorder (localroot. leftchild );
Preorder (localroot. rightchild );
}
}

Private void inorder (node localroot)
{
If (localroot! = NULL)
{
Inorder (localroot. leftchild );
Localroot. displaynode ();
Inorder (localroot. rightchild );
}
}

Private void postorder (node localroot)
{
If (localroot! = NULL)
{
Postorder (localroot. leftchild );
Postorder (localroot. rightchild );
Localroot. displaynode ();
}
}
} // End class tree

 

Public class treeapp
{
Public static void main (string [] ARGs)
{
Tree Thetree = new tree (); // make a tree
Thetree. insert (50, 1.5); // insert 3 nodes
Thetree. insert (25, 1.7 );
Thetree. insert (75, 1.9 );
Node found = Thetree. Find (25); // find node with key 25
If (found! = NULL)
System. Out. println ("found the node with key 25 ");
Else
System. Out. println ("cocould not find node with key 25 ");
// Thetree. Minimum (). displaynode ();
Thetree. printtree ();
System. Out. println ("deleted ...");
Thetree. Delete (25 );
Thetree. printtree ();
} // End main ()
} // End class treeapp

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.