Algorithm (fourth edition) Java implementation of learning notes two fork find tree

Source: Internet
Author: User

Binary search tree: is a binary tree in which each node contains a key and a value associated with it, and each node has a key greater than the key of any node in its left subtree, which is less than the key of any node in its right sub-tree.

The principle of each method of binary search tree is explained in detail in the code, the following is the code:

/** * * @author seabear * Binary search tree * @param <Key> * @param <Value> */public class Bts<key extends comparable& Lt key>,value> {//Definition Tree public class node{//left node private node left;//right node private node right;private Key key;private Value VA lue;private int n;public Node (Key key,value value,int N) {This.key = Key;this.value = Value;this. n = n;}} Root node private node root;//Gets the size of the tree public int size () {return size (root);} public int size (Node root) {if (root = null) {return 0;} return root. N;} Gets the associated value according to the key public value get (key key) {return get (Root,key);} Public Value Get (Node root,key Key) {if (root = null) {return null;} Determines the size of the lookup key associated with the current node key int cmp = ROOT.KEY.COMPARETO (key);//If the lookup key is less than the current node key, continue to find if (CMP < 0) {return get on the left sub-tree of the current node ( Root.left,key);} If the key found is greater than the current node, continue to find the else if (cmp > 0) {return get (Root.right,key) on the right subtree of the current node;} If equal, returns the associated value Else{return Root.value;}} Add a key-value pair to the tree and insert it in the appropriate place public void put (key key,value value) {root = put (root,key,value);} Public node put (node Root,key key,value Value) {if (root = = Null) {return new Node (key,value,1);} The inserted key is compared to the key of the current node int cmp = Root.key.compareTo (key);//If the inserted key is less than the current node's key, continue to insert the IF (CMP < 0) {Root.left = put () into the left subtree of the current node. Root.left,key,value);} If the key inserted is greater than the current node, continue to insert the else if (cmp > 0) {root.right = put (root.right,key,value) to the right subtree of the current node);} If the same key is found, the associated value of the inserted key is replaced with the value of the key associated with the current node Else{root.value = value;} Add the size of the tree to 1root. N = Size (root.left) + size (root.right) + 1;return root;} Gets the smallest key in the tree public Key min () {return min (root). Key;} Public node min (node x) {///If the left subtree of the current node is empty, it indicates that the key of the current node is the smallest key in the tree and returns the node if (X.left = = null) {return x;} Otherwise, continue to find return min (x.left) to the left subtree of the current node;} The key to the given key is rounded down//if the given key is less than two forks to find the root node of the tree,//Then the maximum key for the key is less than or equal to the record of the root node;//If the given key is greater than the root node of the binary lookup tree,// Then only if there is a node in the right subtree of the root node that is less than or equal to the key, the maximum key that is less than or equal to key will appear in the right subtree,//otherwise the root node is the maximum key that is less than or equal to key. Public Key floor (key key) {Node x = floor (Root,key); if (x = = null) {return null;} return X.key;} Public Node Floor (node Root,key Key) {if (root = null) {return null;} int cmp = Key.compareto (Root.key);//If equal, returns the current node if (CMP = = 0) {return root;} continues to recursively find the left subtree of the current node when the given key is less than the current node else if (CMP < 0) {return floor (root.left,key);} When the key of the given key is greater than the current node, the right subtree of the current node continues to traverse, and the node is returned to tnode t = floor (Root.right,key);//If T is not empty, then the maximum key in the right subtree that is less than or equal to key, returns TIF (t! = null) { return t;} Otherwise returns the current node Else{return root;}} Find the key public key select (int key) {return Select (Root,key). Key; Public Node Select (Node X,int key) {if (x = = null) {return null;} int size = size (x.left);//If key is smaller than the node number in the left subtree, then continue recursively looking for the key if (key < size) {return Select (X.left,key) in the left subtree that is ranked K;} If the key is larger than size, we will recursively look for the k-t-1 key else if (key > Size) {return Select (x.right,key-size-1) in the right subtree;} If size equals key, returns the key in the current root node Else{return x;}} Returns the key in the tree according to the given key. public int rank (key key) {return rank (root,key);} public int rank (Node x,key Key) {if (x = = null) {return 0;} int cmp = Key.compareto (X.key);//If the given key is less than the current node's key, it is recursively compared to the left dial hand tree and returns the position of the key in the left subtree (CMP < 0) {return rank (x.left,key);} If the key of the given key is greater than the current node, the total number of nodes in the left subtree plus 1 (root node) plus the rank in the right subtree (recursive calculation) else if (cmp > 0) {return 1 + size (x.left) + rank (x.right,key);} If equal, returns the total number of nodes in the left subtree of the current node Else{return size (X.left);}} Delete minimum key public void DeleteMin () {deletemin (root);} Public node Deletemin (node X) {//If the left subtree of the current node is empty, returns the node of the right subtree of the current node if (X.left = = null) {return x.right;} If the left subtree of the current node is not empty, continue to drill down into the left subtree of the current node until you encounter an empty Zuozi//backtracking to point the link to the right subtree of the node X.left = Deletemin (x.left);//recalculate the size of the tree X. N = Size (x.left) + size (x.right) + 1;return x;} Delete the given key public void Delete (key key) {root = delete (Root,key);} Public Node Delete (node X,key Key) {if (x = = null) {return null;} int cmp = Key.compareto (X.key);//If the given key is less than the current node's key, continue to drill down to the left subtree of the current node if (CMP < 0) {x.left = delete (X.left,key);} If the key for the given key is greater than the current node, continue to the right subtree of the current node, else if (cmp > 0) {x.right = delete (X.right,key);} If equal else{//if the left subtree of the current node is empty, the right subtree node of the current node is returned if (X.left = = null) {return x.right;} If the right subtree of the current node is empty, the left subtree node of the current node is returned if (x.right = = null) {return x.left;} Otherwise://1. Save the link to the node that will be deleted as tnode t = x;//2. Point x to its successor node min (t.right) x = min (t.right);//3. The right link to X ( Originally point to a tree all nodes are greater than x.key two forks find tree) point to Deletemin (T.right), that is, after//delete all nodes are still greater than x.key sub-binary search tree X.right = deletemin (t.right);//4. Set the left link of X (this is empty) to T.left (all keys below it are smaller than the deleted node and its successor) X.left = T.left;} Recalculates the size of the tree X. N = Size (x.left) + size(x.right) + 1;return x;}} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Algorithm (fourth edition) Java implementation of learning notes two fork find tree

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.