JAVA Implementation of Binary Search Tree

Source: Internet
Author: User

 

In order to overcome the fear of tree structure programming, I am determined to implement the Binary Search Tree by myself, so that I can master some skills and methods about tree structure programming. The following are the basic ideas:

 

[1] About containers and encapsulation. Encapsulation is a very important system design concept. process-oriented functions and object-oriented objects are both technical means to implement abstraction and encapsulation. To make the system safer and more maintainability, we should keep the encapsulation idea in mind. Container is an excellent example of encapsulation ideas. The user's impression on the container should be concise: A. You can store the specified items; B. You can retrieve the expected items. What are the organs in this container? The snakes and gold in the container are invisible to users. The binary search tree is such a container. In object-oriented programming, to realize the tree structure, we naturally need to model tree node objects. The internal class is used here. The external class models the binary search tree, and the tree node is used as the internal class implementation.

 

[2] This program tries its best to implement a useful Binary Search Tree, including dynamic insert and delete operations; query given keywords, minimum keywords, and maximum keywords; obtains the ordered list (used for sorting) of Binary Trees. I hope this container can be used in the future, not just programming exercises. Most of the algorithms for binary tree search operations refer to Chapter 2 of Introduction to algorithms. The delete operation is clumsy. There are errors in the program. Please note.

 

[3] The procedure is as follows:

 

/** <Br/> * @ author shuqin1984 2011-3-13 <br/> * This program implements a binary search tree function, which allows you to dynamically insert and delete keywords; <br/> * query the given keywords, minimum keywords, and maximum keywords. convert them to an ordered list (used for sorting) <br/> */<br/> package Datastructure. tree; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> public class binarysearchtree {</P> <p> // root node of the tree <br/> private treenode root = NULL; </P> <p> // traverse the node list <br/> private list <treenode> nodelist = new ARRA Ylist <treenode> (); </P> <p> private class treenode {</P> <p> private int key; <br/> private treenode leftchild; <br/> private treenode rightchild; <br/> private treenode parent; </P> <p> Public treenode (INT key, treenode leftchild, treenode rightchild, treenode parent) {<br/> This. key = key; <br/> This. leftchild = leftchild; <br/> This. rightchild = rightchild; <br/> This. parent = parent; <br/>}< br/> Public int Getkey () {<br/> return key; <br/>}< br/> Public String tostring () <br/>{< br/> string leftkey = (leftchild = NULL? "": String. valueof (leftchild. Key); <br/> string rightkey = (rightchild = NULL? "": String. valueof (rightchild. key); <br/> return "(" + leftkey + "," + key + "," + rightkey + ")"; <br/>}</P> <p>/** <br/> * isempty: determines whether the binary search tree is empty; if it is null, true is returned; otherwise, false is returned. <br/> */<br/> Public Boolean isempty () <br/> {<br/> If (root = NULL) {<br/> return true; <br/>} else {<br/> return false; <br/>}</P> <p>/** <br/> * treeempty: for some Binary Search Tree operations (such as deleting keywords, if the tree is empty, an exception is thrown. <Br/> */<br/> Public void treeempty () throws exception <br/> {<br/> If (isempty ()) {<br/> throw new exception ("the tree is empty! "); <Br/>}</P> <p>/** <br/> * search: query the given keywords in the binary search tree <br/> * @ Param key the given keyword <br/> * @ return matches the tree node with the given keyword <br/> */<br/> Public treenode search (INT key) <br/>{ <br/> treenode pnode = root; <br/> while (pnode! = NULL & pnode. Key! = Key) {<br/> If (Key <pnode. key) {<br/> pnode = pnode. leftchild; <br/>}< br/> else {<br/> pnode = pnode. rightchild; <br/>}< br/> return pnode; <br/>}</P> <p>/** <br/> * minelemnode: obtain the minimum keyword node in the binary search tree <br/> * @ return the minimum keyword node of the Binary Search Tree <br/> * @ throws exception if the tree is empty, an exception is thrown. <br/> */<br/> Public treenode minelemnode (treenode node) throws exception <br/>{< br/> If (node = NULL) {<br/> throw new effectio N ("tree is empty! "); <Br/>}< br/> treenode pnode = node; <br/> while (pnode. leftchild! = NULL) {<br/> pnode = pnode. leftchild; <br/>}< br/> return pnode; <br/>}</P> <p>/** <br/> * maxelemnode: obtain the maximum keyword node in the binary search tree <br/> * @ return the maximum keyword node of the Binary Search Tree <br/> * @ throws exception if the tree is empty, an exception is thrown. <br/> */<br/> Public treenode maxelemnode (treenode node) throws exception <br/>{< br/> If (node = NULL) {<br/> throw new exception ("the tree is empty! "); <Br/>}< br/> treenode pnode = node; <br/> while (pnode. rightchild! = NULL) {<br/> pnode = pnode. rightchild; <br/>}< br/> return pnode; <br/>}</P> <p>/** <br/> * successor: obtain the successor node of a given node in the middle order traversal order <br/> * @ Param node in the given tree <br/> * @ return if the node has a successor in the middle order traversal order node, returns its successor node; otherwise, returns NULL <br/> * @ throws exception <br/> */<br/> Public treenode successor (treenode node) throws exception <br/>{< br/> If (node = NULL) {<br/> return NULL; <br/>}</P> <p> // if the right subtree of the node is not empty, its successor node is the smallest keyword node in the right subtree. <Br/> If (node. rightchild! = NULL) {<br/> return minelemnode (node. rightchild); <br/>}< br/> // if the right subtree of the node is empty <br/> treenode parentnode = node. parent; <br/> while (parentnode! = NULL & node = parentnode. rightchild) {<br/> node = parentnode; <br/> parentnode = parentnode. parent; <br/>}< br/> return parentnode; <br/>}</P> <p>/** <br/> * precessor: obtain the frontend node of a given node in the middle order traversal order <br/> * @ Param node in the given tree <br/> * @ return if the node exists in the middle order traversal order frontend node, otherwise, return NULL <br/> * @ throws exception <br/> */<br/> Public treenode precessor (treenode node) throws exception <br/>{< br/> If (node = NULL) {<Br/> return NULL; <br/>}</P> <p> // If the left subtree of the node is not empty, the forward node is the largest keyword node in the left subtree <br/> If (node. leftchild! = NULL) {<br/> return maxelemnode (node. leftchild); <br/>}< br/> // If the left subtree of the node is empty <br/> treenode parentnode = node. parent; <br/> while (parentnode! = NULL & node = parentnode. leftchild) {<br/> node = parentnode; <br/> parentnode = parentnode. parent; <br/>}< br/> return parentnode; <br/>}</P> <p>/** <br/> * insert: insert the given keyword into the Binary Search Tree <br/> * @ Param key the given keyword <br/> */<br/> Public void insert (INT key) <br/>{< br/> treenode parentnode = NULL; <br/> treenode newnode = new treenode (Key, null ); <br/> treenode pnode = root; <br/> If (root = NULL) {<Br/> root = newnode; <br/> return; <br/>}< br/> while (pnode! = NULL) {<br/> parentnode = pnode; <br/> If (Key <pnode. key) {<br/> pnode = pnode. leftchild; <br/>}< br/> else if (Key> pnode. key) {<br/> pnode = pnode. rightchild; <br/>}else {<br/> // If a node matching a given keyword already exists in the tree, return is not returned directly. <br/> return; <br/>}< br/> If (Key <parentnode. key) {<br/> parentnode. leftchild = newnode; <br/> newnode. parent = parentnode; <br/>}< br/> else {<br/> parentnode. rightchild = newnode; <Br/> newnode. parent = parentnode; <br/>}</P> <p>/** <br/> * insert: delete the tree node matching the given keyword from the Binary Search Tree <br/> * @ Param key given keyword <br/> */<br/> Public void Delete (INT key) throws exception <br/>{< br/> treenode pnode = search (key); <br/> If (pnode = NULL) {<br/> throw new exception ("the keyword to be deleted does not exist in the tree! "); <Br/>}< br/> Delete (pnode); <br/>}</P> <p>/** <br/> * Delete: delete a given node from the binary search tree. <br/> * @ Param pnode the node to be deleted <br/> * prerequisites: the given node already exists in the binary search tree <br/> * @ throws exception <br/> */<br/> private void Delete (treenode pnode) throws exception <br/>{< br/> If (pnode = NULL) {<br/> return; <br/>}< br/> If (pnode. leftchild = NULL & pnode. rightchild = NULL) {// No left or right child node for this node <br/> treenode par Entnode = pnode. parent; <br/> If (pnode = parentnode. leftchild) {<br/> parentnode. leftchild = NULL; <br/>}else {<br/> parentnode. rightchild = NULL; <br/>}< br/> return; <br/>}< br/> If (pnode. leftchild = NULL & pnode. rightchild! = NULL) {// The left child node of the node is empty, and the right child node is not empty <br/> treenode parentnode = pnode. parent; <br/> If (pnode = parentnode. leftchild) {<br/> parentnode. leftchild = pnode. rightchild; <br/> pnode. rightchild. parent = parentnode; <br/>}< br/> else {<br/> parentnode. rightchild = pnode. rightchild; <br/> pnode. rightchild. parent = parentnode; <br/>}< br/> return; <br/>}< br/> If (pnode. leftchild! = NULL & pnode. rightchild = NULL) {// The left child node of the node is not empty, and the right child node is empty <br/> treenode parentnode = pnode. parent; <br/> If (pnode = parentnode. leftchild) {<br/> parentnode. leftchild = pnode. leftchild; <br/> pnode. rightchild. parent = parentnode; <br/>}< br/> else {<br/> parentnode. rightchild = pnode. leftchild; <br/> pnode. rightchild. parent = parentnode; <br/>}< br/> return; <br/>}< br/> // If the left and right child nodes of the node are not empty Delete the successor node of the node and replace it with the successor node <br/> treenode successornode = successor (pnode); <br/> Delete (successornode ); <br/> pnode. key = successornode. key; <br/>}</P> <p>/** <br/> * inordertraverselist: obtain the central traversal node list of the Binary Search Tree <br/> * @ return the central traversal node list of the Binary Search Tree <br/> */<br/> public list <treenode> inordertraverselist () <br/>{< br/> If (nodelist! = NULL) {<br/> nodelist. clear (); <br/>}< br/> inordertraverse (Root); <br/> return nodelist; <br/>}</P> <p>/** <br/> * inordertraverse: traverse the given binary search tree in a central order <br/> * @ Param root the root node of the Binary Search Tree <br/> */<br/> private void inordertraverse (treenode root) <br/>{< br/> If (root! = NULL) {<br/> inordertraverse (root. leftchild); <br/> nodelist. add (Root); <br/> inordertraverse (root. rightchild); <br/>}</P> <p>/** <br/> * tostringoforderlist: get the ordered list of keywords in the binary search tree <br/> * @ return the ordered list of keywords in the binary search tree <br/> */<br/> Public String tostringoforderlist () <br/> {<br/> stringbuilder sbbuilder = new stringbuilder ("["); <br/> for (treenode P: inordertraverselist () {<br/> sbbuilder. append (P. key ); <Br/> sbbuilder. append (""); <br/>}< br/> sbbuilder. append ("]"); <br/> return sbbuilder. tostring (); <br/>}</P> <p>/** <br/> * obtain the string representation of the Binary Search Tree. <br/> */<br/> Public String tostring () <br/> {<br/> stringbuilder sbbuilder = new stringbuilder ("["); <br/> for (treenode P: inordertraverselist () {<br/> sbbuilder. append (p); <br/> sbbuilder. append (""); <br/>}< br/> sbbuilder. append ("]"); <br/> return sbbuilder. Tostring (); <br/>}< br/> Public treenode getroot () {<br/> return root; <br/>}</P> <p> Public static void testnode (binarysearchtree BST, treenode pnode) throws exception {<br/> system. out. println ("local node:" + pnode); <br/> system. out. println ("frontend node:" + BST. precessor (pnode); <br/> system. out. println ("successor node:" + BST. successor (pnode); <br/>}</P> <p> Public static void testtraverse (binarysearchtree BST) {<br /> System. out. println ("binary tree traversal:" + BST); <br/> system. out. println ("Convert binary search tree to ordered list:" + BST. tostringoforderlist (); <br/>}</P> <p> Public static void main (string [] ARGs) <br/>{< br/> try {<br/> binarysearchtree BST = new binarysearchtree (); <br/> system. out. println ("is the search tree empty? "+ (BST. isempty ()? "Yes": "no"); <br/> int [] keys = new int [] {15, 6, 18, 3, 7, 13, 20, 2, 9, 4 }; <br/> for (INT key: Keys) {<br/> BST. insert (key); <br/>}< br/> system. out. println ("is the search tree empty? "+ (BST. isempty ()? "Yes": "no"); </P> <p> treenode minkeynode = BST. minelemnode (BST. getroot (); <br/> system. out. println ("minimum Keyword:" + minkeynode. getkey (); <br/> testnode (BST, minkeynode); </P> <p> treenode maxkeynode = BST. maxelemnode (BST. getroot (); <br/> system. out. println ("Maximum Keyword:" + maxkeynode. getkey (); <br/> testnode (BST, maxkeynode); </P> <p> system. out. println ("root node Keyword:" + BST. getroot (). getkey (); <br/> testnode (BS T, BST. getroot (); <br/> testtraverse (BST); </P> <p> system. out. println ("******************************"); </P> <p> system. out. println ("Search 7:" + (BST. search (7 )! = NULL? "Search successful! ":" An error occurred while searching. This keyword does not exist! "); <Br/> BST. Delete (7); <br/> system. Out. println (" Search 7: "+ (BST. Search (7 )! = NULL? "Search successful! ":" An error occurred while searching. This keyword does not exist! "); <Br/> system. Out. println (" Search 12: "+ (BST. Search (12 )! = NULL? "Search successful! ":" An error occurred while searching. This keyword does not exist! "); <Br/> BST. insert (12); <br/> system. Out. println (" Search 12: "+ (BST. Search (12 )! = NULL? "Search successful! ":" An error occurred while searching. This keyword does not exist! "); </P> <p> testtraverse (BST); </P> <p> system. out. println ("******************************"); </P> <p> BST. insert (16); <br/> BST. delete (6); <br/> BST. delete (4); </P> <p> testtraverse (BST); </P> <p >}catch (exception e) {<br/> system. out. println (E. getmessage (); <br/> E. printstacktrace (); <br/>}</P> <p>}

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.