Binarysearchtree of basic data structure

Source: Internet
Author: User

Problem Description:

Binarysearchtree


Problem Analysis:

The basic implementation


code implementation:

package c04;/** *  @project: datastructureandalgorithmanalysis *  @filename:  binarysearchtree.java *  @version: 0.10 *  @author: jm han *  @date:  18:38 2015/10/19 *  @comment: test purpose *  @result:  */import  c02. Binarysearch;import master. underflowexception;import java.util.comparator;import static tool.util.*;p Ublic class  binarysearchtree<anytype extends comparable<? super anytype>> {    private static class BinaryNode<AnyType>{       binarynode (anytype theelement)       {this (TheElement, null,  null);}       binarynode (anytype theelement, binarynode<anytype> lt ,  binarynode<anytype> rt)       {elemEnt = theelement; left = lt; right = rt;}       AnyType element;      BinaryNode< anytype> left;      binarynode<anytype> right;    }   private BinaryNode<AnyType> root;   public  Binarysearchtree () {      root = null;   }    public void makeempty () {      root = null;    }   public boolean isempty () {      return  Null == root;   }   public boolean contains (AnyType x) {       return contains (x, root);   }    Public anytype findmin () {   &nbsP;  if (IsEmpty ())          throw new  Underflowexception ();       return findmin (Root). element;   }    public anytype findmax () {      if (IsEmpty ())           throw new underflowexception ();       return findmax (Root). element;   }   public void  insert (anytype x) {      root = insert (x, root);    }   public void remove (anytype x) {       root = remove (x, root);   }   public void  Printtree () {      if (IsEmpty ())           system.out.println ("Empty tree");       else         printtree (Root );    }   private boolean contains (anytype x, binarynode< anytype> t) {      if (t == null)           return false;      int comparableresult =  x.compareto (t.element);       if (comparableresult < 0)           return contains (X, t.left);       else if (comparableresult > 0)           return contains (x, t.right);      else          return true;   }   private binarynode <anytype> fiNdmin (binarynode<anytype> t) {      if (null == t)           return null;      else  if (t.left == null)          return t;       return findmax (T.left);   }   private  Binarynode<anytype> findmax (binarynode<anytype> t) {       if (t != null)          while (t.right != null)             t = t.right;       return t;   }   private binarynode<anytype > insert (anytype x, binarynode<anytype> t) {      if (null == t)  &nbsP;       return new binarynode<anytype> (x, null,  NULL);       int compareresult = x.compareto (t.element);       if (compareresult < 0)           t.left = insert (X, t.left);       else if ( compareresult > 0)          t.right =  Insert (X, t.right);      else          ;      return t;   }   private  binarynode<anytype> remove (anytype x, binarynode<anytype> t) {       if (null == t)           return t;     &Nbsp;int compareresult = x.compareto (t.element);       if ( compareresult < 0)          t.left = remove (X, t.left);       else if (compareresult > 0)           t.right = remove (x, t.right);       else if (t.left != null && t.right != null) {          t.element = findmin (t.right) .element;          t.right = remove (t.element, t.right);       }      else          t =  (t.left != null)?t.left:t.right;      return  T;   } &nBsp; private void printtree (binarynode<anytype> t) {       if (null != t) {         printtree (t.left);          system.out.println (t.element);          printtree (t.right);      }   }    public static void main (String[] args)  {       BinarySearchTree<Integer> bst = new BinarySearchTree<Integer> ();       bst.insert (4);       bst.insert (3);       bst.insert (6);       bst.insert (0);       bst.insert (2);       bst.insert (3);       bst.printtree ();     }} 


Binarysearchtree of basic 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.