Complete implementation of Binary Search Tree Data Structure

Source: Internet
Author: User

This article was first introduced here. BST is the basis of data structures such as the self-balancing Binary Tree aVL and B. Therefore, it is necessary to understand the basic nature and operation of BST, if you are not familiar with the BST, you can check the Wiki or refer to Yan Weimin's "data structure and algorithm" or "Introduction to algorithms" to explain the data structure in detail, the following is the Java version of my data query implementation.

Package COM. mars. search; public class binaryseachtree {private node mroot; private node getroot () {return mroot;} public int search (node root, int key) {If (root = NULL) {return 0 xffffffff;} If (Key <root. key) {return search (root. leftnode, key);} If (Key> root. key) {return search (root. rightnode, key) ;}else {return key ;}} public void insert (INT data) {If (mroot = NULL) {mroot = ne W node (data, null, null); // mroot. Key = data;} node root = mroot; while (root! = NULL) {If (Data = root. key) {return;} else if (Data <root. key) {If (root. leftnode = NULL) {root. leftnode = new node (data, null, null); return;} else {root = root. leftnode ;}} else {If (root. rightnode = NULL) {root. rightnode = new node (data, null, null); return;} else {root = root. rightnode; }}/ ** http://courses.csail.mit.edu/6.006/spring11/rec/rec03.pdf Description: * remov E The Node X from the Binary Search Tree, making the necessary * Adjustments to the binary search tree to maintain its properties. (note * that this operation removes a speci encoded ed node from the tree. if you wanted * to delete a key K from the tree, you wowould have to encrypt rst call find (k) to * need nd the node with key K and then call Delete to remove that node) Case 1: * X has no children. just delete it (I. E. change parent node so that it * doesn't point to X) Case 2: X has one child. splice out X by linking X's * parent to X's child case 3: X has two children. splice out X's successor * and replace X with X's successor ** BST Delete analysis can be seen here http://webdocs.cs.ualberta.ca /~ Holte/t26/del-from-bst.html */Public Boolean Delete (INT key) {node root = deletenode (this. mroot, key); If (root! = NULL) {return true;} return false;}/***** It is equivalent: 1. overwrite the value of the leftmost leaf node of the right subtree to the value of the node to be deleted. * then, delete the leftmost leaf node of the right subtree. Find the deleted node in the recursive process, and then assign the * value of the leftmost leaf node of the right subtree to the deleted node, and then delete the leftmost leaf node of the right subtree. This step is done during recursive exit. If you want to ** go through the first if statement **/private node deletenode (node root, int Key), you can take a closer look) {If (Key <root. key) {If (root. leftnode! = NULL) {root. leftnode = deletenode (root. leftnode, key) ;}else {return NULL ;}} else if (Key> root. key) {If (root. rightnode! = NULL) {root. rightnode = deletenode (root. rightnode, key) ;}else {return NULL ;}} else {If (root. leftnode = NULL) {root = root. rightnode;} else if (root. rightnode = NULL) {root = root. leftnode;} else {node newroot = findmin (Root); root. key = newroot. key; // recursive call in this step is equivalent to deleting the left leaf of the leftmost subtree of the right subtree, because leftnode = NULL when recursion exits; // if there is a reference pointing to the parent in the node structure, it is easier for the root node to be deleted. rightnode = deletenode (root. rightnode, Roo T. Key) ;}} return root;} // Private node findmin (node root) {node newroot = root. rightnode; while (newroot. leftnode! = NULL) {newroot = newroot. leftnode;} return newroot;} public void travel (node root) {node = root; If (node = NULL) {return;} travel (node. leftnode); system. out. print (node. key + ""); travel (node. rightnode);} public static class node {int key; node leftnode; node rightnode; node (INT key, node left, node right) {This. key = key; this. leftnode = left; this. rightnode = right ;}} public static void main (string [] ARGs) {binaryseachtree BST = new binaryseachtree (); int [] A = {0, 4, 3, 2, 5, 1, 7, 6, 8, 9}; For (int I: a) {BST. insert (I);} BST. travel (BST. getroot (); int I = BST. search (BST. getroot (), 3); system. out. println ("\ nsearch:" + I); system. out. println ("after delete:"); BST. delete (4); BST. travel (BST. getroot ());}}

Reference http://en.wikipedia.org/wiki/Binary_search_tree

Data structures and algorithms

 

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.