Introduction to algorithms java

Source: Internet
Author: User

The binary search tree is a widely used data structure and is widely used in algorithms. The Binary Search Tree supports multiple dynamic sets of operations, including:

1. Search for a specific value:

2. Minimum value in the binary search tree:

3. Maximum Value in the binary search tree:

4. A node's precursor:

5. successor of a node:

6. insert a specific value:

7. delete a specific value:


In general, in order to ensure the uniqueness of the search and deletion conditions, we assume that the key values of each element in the binary search tree do not need to be equal.

The following describes the structure and method of the Binary Search Tree:

1. Data Structure of the Binary Search Tree:

// Construct the internal class of the binary lookup number, which is equivalent to the structure private class TreeNode {private int key; private TreeNode lchild in the C language; // define the left child node private TreeNode rchild of the binary search tree; // define the right child node private TreeNode parent of the binary search tree; // define the Father's Day Point public TreeNode (int key, TreeNode lchild, TreeNode rchild, TreeNode parent) {this. key = key; this. lchild = lchild; this. rchild = rchild; this. parent = parent;} public int getKey () {return key ;}}


2. Insert a binary search tree:

Public void treeInsert (int key) {// insert TreeNode parentNode = null for binary search; TreeNode newNode = new TreeNode (key, null ); treeNode tmpNode = root; if (root = null) {root = newNode; return;} while (tmpNode! = Null) {// in general, we will not insert the existing node parentNode = tmpNode in the tree; // only the node as an intermediate variable if (key <tmpNode. key) tmpNode = tmpNode. lchild; else if (key> tmpNode. key) tmpNode = tmpNode. rchild; elsereturn;} if (key <parentNode. key) {parentNode. lchild = newNode; newNode. parent = parentNode;} else {parentNode. rchild = newNode; newNode. parent = parentNode ;}}

3. Delete the Binary Search Tree:

Public void treeDelete (int key) {TreeNode tmpNode = treeSearch (key); if (tmpNode = null) System. out. println ("no node to be deleted in this tree! "); Delete (tmpNode); // delete a node} public void delete (TreeNode node) {// key core function for deleting a node if (node = null) return; if (node. lchild = null & node. rchild = null) {// if left and right nodes of the deleted node are empty, TreeNode parentNode = node. parent; if (node = parentNode. lchild) parentNode. lchild = null; elseparentNode. rchild = null; return;} if (node. lchild! = Null & node. rchild = null) {// left child is not empty, right child is empty TreeNode parentNode = node. parent; if (node = parentNode. lchild) {parentNode. lchild = node. lchild; node. lchild. parent = parentNode;} else {parentNode. rchild = node. lchild; node. lchild. parent = parentNode;} return;} if (node. lchild = null & node. rchild! = Null) {// left child is empty, right child is not empty TreeNode parentNode = node. parent; if (node = parentNode. lchild) {parentNode. lchild = node. rchild; node. rchild. parent = parentNode;} else {parentNode. rchild = node. rchild; node. rchild. parent = parentNode;} return;} // left and right children of the node are not empty TreeNode tmpNode = treeSuccessor (node); node. key = tmpNode. key; delete (tmpNode );}

4. Find the minimum element function in the binary tree:

Public TreeNode treeMinNode (TreeNode node) {// get the node of the smallest element in this tree if (node = null) {System. out. println ("this tree is empty! "); Return null;} TreeNode tmpNode = node; while (tmpNode. lchild! = Null) tmpNode = tmpNode. lchild; return tmpNode ;}

5. Find the maximum element in the tree using the binary query function:

Public TreeNode treeMaxNode (TreeNode node) {// obtain the node of the maximum element in this tree if (node = null) {System. out. println ("this tree is empty! "); Return null;} TreeNode tmpNode = node; while (tmpNode. rchild! = Null) tmpNode = tmpNode. rchild; return tmpNode ;}

6. Find the front node of the Binary Tree:

Public TreeNode treePrevious (TreeNode node) {// locate the prefix node of a node in the central order if (node = null) return null; if (node = treeMinNode (root )) // In this case, if a node is the smallest element, return null is not returned for the previous node; if (node. lchild! = Null) return treeMaxNode (node. lchild); else {TreeNode parentNode = node. parent; while (parentNode! = Null & node = parentNode. lchild) {node = parentNode; parentNode = parentNode. parent;} return parentNode ;}}

7. Find the successor node of the Binary Tree:

Public TreeNode treeSuccessor (TreeNode node) {// locate the subsequent node of a node in the central order if (node = null) return null; if (node = treeMaxNode (root )) // In this case, if a node is the largest element, no subsequent node returns null; if (node. rchild! = Null) // when the right subtree of this node is not empty return treeMinNode (node. rchild); else {// when the right subtree of this node is empty, TreeNode parentNode = node. parent; while (parentNode! = Null & node = parentNode. rchild) {node = parentNode; parentNode = parentNode. parent;} return parentNode ;}}


8. traverse the binary search tree in the middle order:

Public void inOrderTranvers (TreeNode node) {// This is a forward traversal of the binary search tree if (node! = Null) {inOrderTranvers (node. lchild); nodelist. add (node); inOrderTranvers (node. rchild) ;}} public List
 
  
InOrderTree () {// obtain the nodes that are sequentially traversed in the binary search tree. if (nodelist! = Null) nodelist. clear (); inOrderTranvers (root); return nodelist ;}
 

9. Print the Binary Search Tree:

Public String printTree () {// print this binary search tree List
 
  
Ll = inOrderTree (); Iterator
  
   
It = ll. iterator (); StringBuilder sb = new StringBuilder (); while (it. hasNext () {TreeNode p = (TreeNode) it. next (); sb. append (p. key + "");} return sb. toString ();}
  
 

The complete source code is provided below:

Package binaryTree; import java. util. arrayList; import java. util. iterator; import java. util. list;/** implement a binary search count for insert, delete, search, and other operations */public class BinarySearchTree {private TreeNode root = null; // define the root node private List of the tree
 
  
Nodelist = new ArrayList
  
   
(); // The nodes in the traversal table // construct the internal class of the binary lookup number, which is equivalent to the structure private class TreeNode {private int key; private TreeNode lchild in the C language; // define the left child node private TreeNode rchild of the binary search tree; // define the right child node private TreeNode parent of the binary search tree; // define the Father's Day Point public TreeNode (int key, TreeNode lchild, TreeNode rchild, TreeNode parent) {this. key = key; this. lchild = lchild; this. rchild = rchild; this. parent = parent;} public int getKey () {return key;} public boole An isEmpty () {// determines whether the binary search tree is empty return root = null;} public void treeInsert (int key) {// insert TreeNode parentNode = null; TreeNode newNode = new TreeNode (key, null); TreeNode tmpNode = root; if (root = null) {root = newNode; return;} while (tmpNode! = Null) {// in general, we will not insert the existing node parentNode = tmpNode in the tree; // only the node as an intermediate variable if (key <tmpNode. key) tmpNode = tmpNode. lchild; else if (key> tmpNode. key) tmpNode = tmpNode. rchild; elsereturn;} if (key <parentNode. key) {parentNode. lchild = newNode; newNode. parent = parentNode;} else {parentNode. rchild = newNode; newNode. parent = parentNode;} public TreeNode treeSearch (int key) {// implement TreeNode tmpNode = Root; while (tmpNode! = Null & tmpNode. key! = Key) {if (key <tmpNode. key) tmpNode = tmpNode. lchild; elsetmpNode = tmpNode. rchild;} return tmpNode;} public TreeNode treeMinNode (TreeNode node) {// get the node of the smallest element in this tree if (node = null) {System. out. println ("this tree is empty! "); Return null;} TreeNode tmpNode = node; while (tmpNode. lchild! = Null) tmpNode = tmpNode. lchild; return tmpNode;} public TreeNode treeMaxNode (TreeNode node) {// obtain the node where the maximum element in the tree is if (node = null) {System. out. println ("this tree is empty! "); Return null;} TreeNode tmpNode = node; while (tmpNode. rchild! = Null) tmpNode = tmpNode. rchild; return tmpNode;} public TreeNode treeSuccessor (TreeNode node) {// find the subsequent node of a node in the central order if (node = null) return null; if (node = treeMaxNode (root) // In this case, the node is the largest element, and no subsequent node return null; if (node. root. rchild! = Null) // when the right subtree of this node is not empty return treeMinNode (node. rchild); else {// when the right subtree of this node is empty, TreeNode parentNode = node. parent; while (parentNode! = Null & node = parentNode. rchild) {node = parentNode; parentNode = parentNode. parent;} return parentNode;} public TreeNode treePrevious (TreeNode node) {// locate the prefix node of a node in the central order if (node = null) return null; if (node = treeMinNode (root) // in this case, there is a special case that the node is the smallest element of the node, and no previous node return null; if (node. lchild! = Null) return treeMaxNode (node. lchild); else {TreeNode parentNode = node. parent; while (parentNode! = Null & node = parentNode. lchild) {node = parentNode; parentNode = parentNode. parent;} return parentNode;} public void inOrderTranvers (TreeNode node) {// This is a forward traversal of the if (node! = Null) {inOrderTranvers (node. lchild); nodelist. add (node); inOrderTranvers (node. rchild) ;}} public List
   
    
InOrderTree () {// obtain the nodes that are sequentially traversed in the binary search tree. if (nodelist! = Null) nodelist. clear (); inOrderTranvers (root); return nodelist;} public void treeDelete (int key) {TreeNode tmpNode = treeSearch (key); if (tmpNode = null) System. out. println ("no node to be deleted in this tree! "); Delete (tmpNode); // delete a node} public void delete (TreeNode node) {// key core function for deleting a node if (node = null) return; if (node. lchild = null & node. rchild = null) {// if left and right nodes of the deleted node are empty, TreeNode parentNode = node. parent; if (node = parentNode. lchild) parentNode. lchild = null; elseparentNode. rchild = null; return;} if (node. lchild! = Null & node. rchild = null) {// left child is not empty, right child is empty TreeNode parentNode = node. parent; if (node = parentNode. lchild) {parentNode. lchild = node. lchild; node. lchild. parent = parentNode;} else {parentNode. rchild = node. lchild; node. lchild. parent = parentNode;} return;} if (node. lchild = null & node. rchild! = Null) {// left child is empty, right child is not empty TreeNode parentNode = node. parent; if (node = parentNode. lchild) {parentNode. lchild = node. rchild; node. rchild. parent = parentNode;} else {parentNode. rchild = node. rchild; node. rchild. parent = parentNode;} return;} // left and right children of the node are not empty TreeNode tmpNode = treeSuccessor (node); node. key = tmpNode. key; delete (tmpNode);} public TreeNode getRoot () {// get the root node return root;} public String printTree () {// print out this binary search tree List
    
     
Ll = inOrderTree (); Iterator
     
      
It = ll. iterator (); StringBuilder sb = new StringBuilder (); while (it. hasNext () {TreeNode p = (TreeNode) it. next (); sb. append (p. key + "");} return sb. toString ();} public static void main (String [] args) {BinarySearchTree bst = new BinarySearchTree (); System. out. println ("Whether the binary search tree is empty:" + bst. isEmpty (); int [] keys = new int [] {15, 6, 18, 3, 7, 13, 20, 2, 9, 4 }; for (int key: keys) bst. treeInsert (key); duplicate E M. out. println ("Whether the binary search tree is empty:" + bst. isEmpty (); System. out. println ("the smallest element in the binary search tree is:" + bst. treeMinNode (bst. getRoot ()). getKey (); System. out. println ("the maximum element in the binary search tree is:" + bst. treeMaxNode (bst. getRoot ()). getKey (); System. out. println ("the element of the binary tree root node is:" + bst. getRoot (). getKey (); System. out. println ("the binary search tree is:" + bst. printTree (); System. out. println ("check whether 9 is in the binary search tree:" + (bst. treeSearch (9 )! = Null )? "In": "not"); bst. treeDelete (9); System. out. println ("check whether 9 is in the binary search tree:" + (bst. treeSearch (9 )! = Null )? "In": "not"); System. out. println ("the binary search tree is:" + bst. printTree (); System. out. println ("Search 13 in the binary search tree:" + (bst. treeSearch (13 )! = Null )? "In": "not "));}}
     
    
   
  
 

Run the following command:



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.