0
private static void duplicate(AVLNode bTree)
{
   if(bTree != null)
   {
      if(bTree.left == bTree.right)
      {            
         duplicateNum += bTree.value + " ";
      }
   }
}

I'm trying to write a method that finds duplicate values in an AVL Tree and displays them to a JTextField. What am I missing in the code provided? Thanks for all your help!

Jeremy
  • 113
  • 2
  • 14
  • 1
    I don't know how you made it but basically you can't have duplicate keys. You could use a list of different values for that key. But you can't have different nodes with the same key. Or you can use other techniques like discribed here : http://stackoverflow.com/questions/2472964/handling-duplicates-keys-within-an-avl-tree – Kevin Apr 26 '15 at 22:21

1 Answers1

0

The class "node" could look something like this :

public class Node<K, V> {
    public Node left_child, right_child;
    public K key;
    public ArrayList<V> value; // Now each key holds a list of values

    // Make a subclass "NullNode" which indicates it isn't a node.
    // So that left/right childs can be ommitted by passing the NullNode (e.g for leaf nodes)
    public Node(Node left, Node right, K key, V value) {
        this.left_child = left;
        this.right_child = right;
        this.key = key;
        this.value.add(value);
    }

    // Get the node's values
    public ArrayList<V> values() {
        this.values;
    }

    // Are we a leaf node ? --> No
    public boolean leaf() {
        return false;
    }

    // Other needed procedures
}

public class NullNode extends Node<int, int> {
    public NullNode() {
         super(0, 0); // Just because we have to pass values
    }

    // We are a leaf node
    public boolean leaf() {
        return true;
    }
}

Before doing operations on nodes you then always first have to be sure it is not a leaf node, using leaf() method.

Notice that this is just to help you a bit further, it can be written cleaner then this.

Kevin
  • 2,813
  • 3
  • 20
  • 30