Binary search Tree--java

Source: Internet
Author: User

 PackageCom.test.tree; Public classBinarysearchtree<textendscomparable<?SuperT>> {    /*define the nodes of a binary tree*/    Private classBinarynode<t>{         PublicT data;  PublicBinarynode<t>lt;  PublicBinarynode<t>RT;  PublicBinarynode (T data) { This(Data,NULL,NULL); }         PublicBinarynode (T data, Binarynode<t> LT, binarynode<t>RT) {             This. data =data;  This. Lt =lt;  This. RT =RT; }    }        PrivateBinarynode<t> Root;//define the root node of a binary lookup tree         PublicBinarysearchtree () { // Initialize binary lookup tree root=NULL; }         Public voidMakeempty () {//Tree empty root=NULL; }         Public BooleanIsEmpty () {//Tree emptyreturnRoot = =NULL; }         Public Booleancontains (T x) {//To determine if a value is includedreturncontains (root, x); }     Public BooleanContains (binarynode<t>root, T x) {        if(Root = =NULL){            return false; }        intCompare =X.compareto (Root.data); if(Compare = = 0){            return true; }Else if(Compare < 0) {contains (root.lt, x); }Else{contains (root.rt, x); }        return false; }         PublicT findmin () {//Get the lowest value in the treeif(!IsEmpty ()) {            returnfindmin (root). data; }        return NULL; }     PublicT Findmax () { //Get maximum in tree         if(!IsEmpty ()) {            returnFindmax (root). data; }        return NULL; }         Public voidInsert (T data) {//Inserting data root=Insert (data, root); }         Public voidRemove (T data) {root=Remove (data, root); }         Public voidPrinttree () {if(Root = =NULL) {System.out.println ("Empty Tree"); }Else{printtree (root); }    }    /*Middle Sequence Traversal*/     Public voidPrinttree (binarynode<t>t) {        if(t! =NULL) {printtree (t.lt); System.out.print (T.data+ ",");        Printtree (T.RT); }    }    /*** Delete a node of the lookup tree, first replace the node value with the minimum value in the right subtree of the node you want to delete, and then delete the node from the right subtree, recursively call **/     Publicbinarynode<t> Remove (T data, binarynode<t>t) {        if(T = =NULL){            returnT; }        intCompare =Data.compareto (T.data); if(Compare < 0){            //insertion value is smaller than the value of the root node and inserted into the left word countt.lt =Remove (data, t.lt); }Else if(Compare > 0){            //insertion value is smaller than the value of the root node and inserted into the left word countT.rt =Remove (data, t.rt); }Else if(t.lt! =NULL&& T.rt! =NULL) {T.data= Findmin (T.RT). data;//assigns the smallest value in the right subtree to the node to be deletedT.rt =Remove (T.data, t.rt); }Else{T= T.lt = =NULL?t.rt:t.lt; }        returnT; }     Publicbinarynode<t> Insert (T data, binarynode<t>t) {        if(T = =NULL){            return NewBinarynode<t> (data,NULL,NULL); }        intCompare =Data.compareto (T.data); if(Compare < 0){            //insertion value is smaller than the value of the root node and inserted into the left word countt.lt =Insert (data, t.lt); }Else if(Compare > 0){            //insertion value is smaller than the value of the root node and inserted into the left word countT.rt =Insert (data, T.RT); }Else{        }        returnT; }     PublicBinarynode<t> Findmin (binarynode<t>t) {        if(T = =NULL){            returnT; }Else if(t.lt = =NULL){//the left side of the lookup tree is smaller than the node value, so you can find the leftmost node            returnT; }Else{            returnfindmin (t.lt); }    }         PublicBinarynode<t> Findmax (binarynode<t>t) {        if(T = =NULL){            return NULL; }Else if(T.rt = =NULL){//The right side of the lookup tree is larger than the node value, so you can find the rightmost node            returnT; }        returnFindmax (T.RT); }         Public Static voidMain (string[] args) {Binarysearchtree<Integer> Binarysearchtree =NewBinarysearchtree<integer>(); Binarysearchtree.insert (8); Binarysearchtree.insert (4); Binarysearchtree.insert (6); Binarysearchtree.insert (3); Binarysearchtree.insert (14); Binarysearchtree.insert (10); System.out.println ("Minimum value:" +binarysearchtree.findmin ()); System.out.println ("Maximum value:" +Binarysearchtree.findmax ());        Binarysearchtree.printtree (); Binarysearchtree.remove (8);        System.out.println ();    Binarysearchtree.printtree (); }}

Binary search Tree--java

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.