Data structure--Java implementation of two-fork search tree

Source: Internet
Author: User

On the code:
Package Com.itany.erchachazhaoshu;public class Binarysearchtree<t extends Comparable<?    Super t>>{//define binary lookup tree root node each lookup binary tree has a root node of its own the outside world does not see the private binarynode<t> root;    Public Binarysearchtree () {root=null;        }//Node class private static class Binarynode<t> {private T element;        Private binarynode<t> left;        Private binarynode<t> right;        Public Binarynode (T element) {This (element, NULL, NULL); } public Binarynode (T element,binarynode<t> left,binarynode<t> right) {this.element=            Element            This.left=left;        This.right=right;    }} public void Makeempty () {root=null;    } public boolean IsEmpty () {return root==null;    } public Boolean contains (T-t) {return contains (t,root); }//The outside world is not aware of the node will only return T Boolean or no return value at all public T Findmax () throws Exception {if (isempTy ()) throw new Exception ();    return Findmax (root). element;        } public T Findmin () throws Exception {if (IsEmpty ()) throw new Exception ();    return Findmin (root). element;    } public void Insert (T t) {Root=insert (t,root);    } public void remove (T t) {root=remove (t,root); }//used here is the tail recursive private Boolean contains (T t,binarynode<t> root) {//must be judged at the outset if null otherwise the method or element will be called when NULL is generated        A pointer exception is also a datum condition if (root==null) return false;        int Compareres=t.compareto (root.element);        if (compareres==0) return true; else if (compareres<0) return contains (t,root.left);//does not cause the stack to go in and out only overwrites the current stack else return contains    (T,root.right); }//Method two use loops instead of tail recursion to find out the maximum is to return the corresponding node of the private binarynode<t> Findmax (binarynode<t> root) {if (root=        =null) return null;           else {while (root.right!=null) {root=root.right;    }} return root; }//Method one uses recursive lookup to return a reference to the minimum node private binarynode<t> findmin (binarynode<t> root) {//must be judged at the outset whether it is nul        l otherwise null pointer exception if (ROOT==NULL) return null if method or element is called;        Benchmark condition else if (root.left==null) return root;    else return findmin (root.left);            }//Returns an entire node after it has been inserted returns the private binarynode<t> insert (T t,binarynode<t> root) {if (root==null)        return new binarynode<t> (t,null,null);            else {int Com=t.compareto (root.element);            if (com==0);            else if (com<0)//constantly update the left value of the current root and return to root Root.left=insert (t,root.left);            else Root.right=insert (t,root.right);        return root;    }}//delete and add the same as all to return the modified node private binarynode<t> remove (T t,binarynode<t> root) {    Do not find what to delete and do not directly return the root if (root==null) return root;        int Com=t.compareto (root.element);        if (com<0) {root.left=remove (t,root.left);        } else if (com>0) {root.right=remove (t,root.right);                } else {//have two sons if (Root.left!=null && root.right!=null) {                Root.element=findmin (root.right). element; Root.right=remove (root.element,root.right);//After the update is removed} else//includes a son who has no situation with a son in the case ret                        Urn (root.left!=null)? Root.left:root.right;    } return root; }}
Package Com.itany.erchachazhaoshu;public class test{public        static void Main (string[] args)    {        Binarysearchtree bt=new Binarysearchtree ();        Bt.insert (3);        Bt.insert (+);        Bt.insert (1);        Try        {            System.out.println ("Max:" +bt.findmax ());            System.out.println ("Max:" +bt.findmin ());            System.out.println (Bt.contains (3));             Bt.remove (+);            System.out.println (bt.contains);        }        catch (Exception e)        {            e.printstacktrace ();}}    }


Data structure--Java implementation of two-fork search 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.