Avltree of basic data structure

Source: Internet
Author: User

Problem Description:

Avltree


Problem Analysis:

The basic implementation

Code implementation:

package c04;/** *  @project: datastructureandalgorithmanalysis *  @filename:  avltree.java *  @version: 0.10 *  @author: jm han *  @date:  15:04 2015/10/21 *  @comment: test purpose *  @result: */import  Master. underflowexception;import static tool.util.*;p Ublic class avltree<anytype extends  Comparable<? super AnyType>>{   private static final  int allowed_imbalance = 1;   private avlnode<anytype> root;    private static class AvlNode<AnyType>{       avlnode (anytype theelement)       {this (theElement, null,  NULL);}       avlnode (anytype theelement, avlnode lt, avlnode  RT)   &Nbsp;   {element = theelement; left = lt; right = rt;}       AnyType element;      AvlNode< anytype> left;      avlnode<anytype> right;       int height;   }   public int height (AvlNode< ANYTYPE&GT;&NBSP;T) {      return t == null?-1:t.height;    }   public boolean isempty () {      return  null == root;   }   public void insert (AnyType x) {      root = insert (x,root);   }    Public void remove (anytype x) {      root = remove (x, root);    }    public anytype findmin () {      if (IsEmpty ())           throw new underflowexception ();       return findmin (Root) .element;   }   public anytype  Findmax () {      if (IsEmpty ())           throw new underflowexception ();       return findmax (Root). Element;   }   public boolean contains (AnyType x) {       return contains (x, root);   }   public  Void printtree () {      if (IsEmpty ())           system.out.println ("Empty tree");      else          printtree (Root);   }   private avlnode<anytype>  Insert (anytype x, avlnode<anytype> t) {      if (null == &NBSP;T)          return new AvlNode<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          ;//Duplicat, do nothing      return  Balance (t);    } &nbsP; private avlnode<anytype> balance (avlnode<anytype> t)  {       if  (null == t)           return t;      if  (height (t.left)  - height (t.right)   > allowed_imbalance)  {         if  (height ( T.left.left)  >= height (t.left.right))              t = rotatewithleftchild (t);          else             t = doublewithleftchild (t);       }      else if (height (t.right)  -  height (t.left)  > allowed_imbalance) {          if (height (t.right.right) &NBSP;&GT;= height (t.right.left))             t =  rotatewithrightchild (t);         else             t = doublewithrightchild (t);       }      //still need to update T's height     when not doing rotate operation   t.height = math.max (height (t.left),  height (t.right))  + 1;       return t;   }   private AvlNode<AnyType>  rotatewithleftchild (AVLNODE&LT;ANYTYPE&GT;&NBSP;K2) {      avlnode< anytype> k1 = k2.left;      k2.left = k1.right;       k1.right = k2;      k2.height  = math.max (Height (k2.left),  height (k2.right))  + 1;      k1.height = math.max ( Height (k1.left),  k2.height)  + 1;      return k1;    }   private avlnode<anytype> rotatewithrightchild (AvlNode<AnyType> &NBSP;K2) {      avlnode<anytype> k1 = k2.right;       k2.right = k1.left;      k1.left =  K2;      k2.height = math.max (height (k2.left),  height (K2.right))  + 1;      k1.height = math.max (height (k1.right),  K2.height)  + 1;      return k1;   }    private avlnode<anytype> doublewithleftchild (AVLNODE&LT;ANYTYPE&GT;&NBSP;K3) {        K3.left = rotatewithrightchild (K3.left);      return  Rotatewithleftchild (K3);   }   private avlnode<anytype>  Doublewithrightchild (AVLNODE&LT;ANYTYPE&GT;&NBSP;K3) {      k3.right =  Rotatewithleftchild (k3.right);       return rotatewithrightchild (K3);    }   private avlnode<anytype> remove (AnyType x, AvlNode <anytype> t) {      if (null == t)           return t;      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 balance (t);   }   private avlnode<anytype>  Findmin (avlnode<anytype> t) {      if (null == t)           return null;      else if ( T.left == null)          return t;      return  Findmin (T.left);    }   private avlnode<anytype> findmax (AvlNode <anytype> t) {      if (t != null)           while (t.right != null)              t = t.right;      return t;    }   private boolean contains (anytype x, avlnode<anytype> t ) {      if (t == null)           return false;      int comparableresult = x.compareto ( t.element);       if (comparableresult < 0)         &Nbsp; return contains (X, t.left);       else if ( comparableresult > 0)          return contains (x ,  t.right)  ;      else          return true;   }   private void printtree (AvlNode< anytype> t) {      if (null != t) {          printtree (T.left);          System.out.println (t.element);          printtree (t.right);       }   }   public static void main ( String[] args)  {      avltree<integer> avlt = new  AvlTree<Integer> (&NBSP;&NBSP;&NBS);P;   avlt.insert (4);       avlt.insert (3);       avlt.insert (6);       avlt.insert (0);       avlt.insert (2);       avlt.insert (3);       Avlt.printtree ();       system.out.println ("contains 3: "  +  Avlt.contains (3));       system.out.println ("max: "  + avlt.findMax ());       system.out.println ("min: "  + avlt.findmin ());       avlt.remove (3);       avlt.printtree ();    }}

Code output:

02346Contains 3:truemax:6min:00246


Avltree 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.