Algorithm Series (eight) binary search tree of data structure

Source: Internet
Author: User
Tags comparable

The basic structure of the Tree of the algorithm series (vii) and the traversal of the two-fork tree are introduced, and the implementation and traversal of the binary tree are presented. This article focuses on learning about binary search trees. OverviewThe two-fork sort tree, also known as a binary search tree, is a binary lookup tree.
Binary search tree (BST) is an important application of the two-fork tree, it adds a property on the basis of a binary tree: For each node in the tree, if there is a left son, the value of its left son must be less than its own value, and if there is a right son, the value of its right son must be greater than its own value. Binary search tree operations generally have insert, delete and find, the average time complexity of these operations are O (LOGN), insert and find operations is simple, delete operation is a bit more complicated.
due to the recursive definition of the tree, usually recursive implementation of some operations, binary search tree average depth is O (logn), do not worry about stack overflow. definition Two forks The search tree is an empty tree, or a two-fork tree with the following properties:
(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) The left and right sub-trees are also two-fork search trees respectively;
(4) There are no nodes with the same key value.

Code Implementation Basics

Inherits the tree structure from the previous article and adds a comparator. There must be a clear node comparison method, otherwise the basic operation cannot be performed.

Package Com.algorithm.tree;import java.util.comparator;/** * Left subtree is less than right subtree of two forks find tree *  * @author Chao * * @param <T> */pu Blic class Binaryseachtree<t> extends binarytree<t> {private comparator<t> Comparator;public Binaryseachtree (binaryseachtree.binarytreenode<t> root) {This (root, null);} Public Binaryseachtree (binaryseachtree.binarytreenode<t> root, comparator<t> Comparator) {super (root); This.comparator = Comparator;} private int Compare (binaryseachtree.binarytreenode<t> A, binaryseachtree.binarytreenode<t> b) {if ( Comparator! = null) {return Comparator.compare (a.element, b.element);} else if (a.element instanceof comparable && ; B.element instanceof Comparable) {return ((comparable) a.element). CompareTo (b.element);} else {throw new RuntimeException ("Can ' t compare" + a.element.getclass ());}}}

Insert OperationStep: Insert the X node into the tree T, the current comparison node is cur, and the insertion node is x,r. By default, the initial node is compared from the root node, and the Cur initial value is root,If the root node is empty, X is used directly as the root node. Otherwise compare the size of cur with x, if x is larger than cur, the x is on the right subtree of cur, and cur.right is the initial node, continue doing the same operation. If cur is larger than x, it means that X is on the left subtree of cur, with cur.left as the initial node, continuing to do the same. Code Implementation
/** * Insert a new data from the root node *  * @param x * @return */public binarytreenode<t> Insert (binarytreenode<t> x) {return in SERT (root, x);} /** * Insert new data from a node * *  @param cur * @param x * @return */protected binarytreenode<t> Insert (BINARYTREENODE<T&G T Cur, binarytreenode<t> x) {if (cur = = null) {return x;} int compareresult = Compare (cur, x), if (Compareresult < 0) {cur.right = insert (cur.right, x);} else if (Compareresult & Gt 0) {cur.left = insert (cur.left, x);} return cur;}

find maximum and minimum valuesby definition, it is known that the minimum value is located on the leftmost leaf node. The maximum value is located at the far right of the leaf node. Code Implementation
/** * Find Minimum node *  * @return */public binarytreenode<t> findmin () {return findmin (root);}  /** * node as the root node of the minimum nodes *  * @param node * @return */public binarytreenode<t> findmin (binarytreenode<t> node) {if (node = = null) {return null;} else if (Node.left = = null) {return node;} Return Findmin (node.left);} /** * Find Maximum node *  * @return */public binarytreenode<t> Findmax () {return findmin (root);}  /** * node as the root node of the maximum nodes *  * @param node * @return */public binarytreenode<t> Findmax (binarytreenode<t> node) {if (node = = null) {return null;} else if (node.right = = null) {return node;} Return Findmin (node.right);}

find out if an action is included with a nodethe meaning of a bit of binary search. Just the binary lookup is linear, and the lookup tree is found on a tree-shaped structure. Code implementation:
/** * Determines if x is included *  * @param x * @return */public Boolean contains (Binarytreenode<t> x) {return contains (root, x);} /** * To determine if a node is the root node contains x *  * @param cur * @param x * @return */public boolean contains (binarytreenode<t> cur, Bina Rytreenode<t> x) {if (x = = NULL | | cur = = NULL) {return false;} int compareresult = Compare (cur, x), if (compareresult = = 0) {return true;} else if (Compareresult < 0) {return contains (Cur.right, x);} else {return contains (cur.left, x);}}



Delete OperationThe delete operation requires a discussion of the situation:if it is a leaf node, it can be deleted directly. If there is only one child, let its father point to its son and delete the node.

If there are two children, the general deletion strategy is to use the smallest right subtree data instead of the node's data, recursively delete that node. Code Implementation
/** * Delete node x *  * @param x * @return */public binarytreenode<t> Remove (binarytreenode<t> x) {return Remove (R Oot, x);} /** * Delete the nodes in the subtree x * *  @param cur * @param x * @return */public binarytreenode<t> Remove (binarytreenode<t> cu R, binarytreenode<t> x) {if (cur = = NULL | | x = = NULL) {return cur;} int compareresult = Compare (cur, x), if (Compareresult > 0) {cur.left = remove (Cur.left, x),} else if (Compareresult < ; 0) {cur.right = remove (cur.right, x);} else if (cur.left! = NULL && cur.right! = null) {cur.right = remove (Cur.rig HT, Findmin (Cur.right));} else {cur = (Cur.left = = null)? Cur.right:cur.left;} return cur;}

The process of deletion is more complex, and if the number of deletions is not many, the usual strategy is to use lazy deletion, which is to mark the deleted node. In particular, it is useful to have duplicates, removing only the frequency of the items minus 1. Not for the moment.


code implementation can look at GitHub, address Https://github.com/robertjc/simplealgorithmGitHub code is also constantly improving, some places may have problems, but also please more advice

Welcome to scan QR Code, follow public account


Algorithm Series (eight) binary search tree of data structure

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.