DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Having hard time to run this one

    Code:
    package net.datastructures;
    import java.util.Comparator;
    
    /**  Implementation of an AVL tree. */
    /** 
     * AVLTree class - implements an AVL Tree by extending a binary
     * search tree.
     *
     * @author Michael Goodrich, Roberto Tamassia, Eric Zamore
     */
    
    public class AVLTree extends BinarySearchTree implements Dictionary {
      public AVLTree(Comparator c)  { super(c); }
      public AVLTree() { super(); }
      /** Nested class for the nodes of an AVL tree. */ 
      protected static class AVLNode extends BTNode {
        protected int height;  // we add a height field to a BTNode
        AVLNode() {/* default constructor */}
        /** Preferred constructor */
        AVLNode(Object element, BTPosition parent,
    	    BTPosition left, BTPosition right) {
          super(element, parent, left, right);
          height = 0;
          if (left != null) 
            height = Math.max(height, 1 + ((AVLNode) left).getHeight());
          if (right != null) 
            height = Math.max(height, 1 + ((AVLNode) right).getHeight());
        } // we assume that the parent will revise its height if needed
        public void setHeight(int h) { height = h; }
        public int getHeight() { return height; }
      }
      /** Creates a new binary search tree node (overrides super's version). */
      protected BTPosition createNode(Object element, BTPosition parent,
                  BTPosition left, BTPosition right) {
        return new AVLNode(element,parent,left,right);  // now use AVL nodes
      }
      /** Returns the height of a node (call back to an AVLNode). */
      protected int height(Position p)  {
        return ((AVLNode) p).getHeight();
      }
      /** Sets the height of an internal node (call back to an AVLNode). */
      protected void setHeight(Position p)  { // called only if p is internal
        ((AVLNode) p).setHeight(1+Math.max(height(left(p)), height(right(p))));
      }
      /** Returns whether a node has balance factor between -1 and 1. */
      protected boolean isBalanced(Position p)  {
        int bf = height(left(p)) - height(right(p));
        return ((-1 <= bf) &&  (bf <= 1));
      }
      /** Returns a child of p with height no smaller than that of the other child */
      /** 
        * Return a child of p with height no smaller than that of the
        * other child.
        */
      protected Position tallerChild(Position p)  {
        if (height(left(p)) > height(right(p))) return left(p);
        else if (height(left(p)) < height(right(p))) return right(p);
        // equal height children - break tie using parent's type
        if (isRoot(p)) return left(p);
        if (p == left(parent(p))) return left(p);
        else return right(p);
      }
      /**  
        * Rebalance method called by insert and remove.  Traverses the path from 
        * zPos to the root. For each node encountered, we recompute its height 
        * and perform a trinode restructuring if it's unbalanced.
        */
      protected void rebalance(Position zPos) {
        if(isInternal(zPos))
           setHeight(zPos);
        while (!isRoot(zPos)) {  // traverse up the tree towards the root
          zPos = parent(zPos);
          setHeight(zPos);
          if (!isBalanced(zPos)) { 
    	// perform a trinode restructuring at zPos's tallest grandchild
            Position xPos =  tallerChild(tallerChild(zPos));
            zPos = restructure(xPos); // tri-node restructure (from parent class)
            setHeight(left(zPos));  // recompute heights
            setHeight(right(zPos));
            setHeight(zPos);
          }
        }
      } 
      // overridden methods of the dictionary ADT
      /** 
        * Inserts an item into the dictionary and returns the newly created
        * entry. 
        */
      public Entry insert(Object k, Object v) throws InvalidKeyException  {
        Entry toReturn = super.insert(k, v); // calls our new createNode method
        rebalance(actionPos); // rebalance up from the insertion position
        return toReturn;
      }
      /** Removes and returns an entry from the dictionary. */
      public Entry remove(Entry ent) throws InvalidEntryException {
        Entry toReturn = super.remove(ent);
        if (toReturn != null)   // we actually removed something
          rebalance(actionPos);  // rebalance up the tree
        return toReturn;
      }
    } // end of AVLTree class
    I got many error messages.
    Your help is appreciated.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Can you post the error messages?

  3. #3
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Package is attached.

    It looks like I have to have a package and I am missing many classes.
    Please see attached files.
    I am using the javac AVLTree\*.java
    I am getting 82 errors, so I could not read the first 40 errors. DOS does not permit me to see them.
    Attached Files

Similar Threads

  1. VB6: ADODC design time vs run time database
    By Tim Johnson in forum VB Classic
    Replies: 4
    Last Post: 09-12-2002, 09:27 AM
  2. Run Time Scripting Doesn't Work in Windos Service
    By Victor Maynard in forum .NET
    Replies: 0
    Last Post: 05-21-2002, 10:37 AM
  3. Beta2 Southern Hemisphere Blues
    By Esmond Hart in forum .NET
    Replies: 2
    Last Post: 01-14-2002, 07:24 AM
  4. Run time error 3704.
    By Rajendra KASHI in forum VB Classic
    Replies: 1
    Last Post: 11-13-2000, 09:01 AM
  5. VB Run Time Error 3706.
    By Lytech in forum VB Classic
    Replies: 4
    Last Post: 10-20-2000, 12:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links