java - How do you handle duplicates in binary tree? -


this testing part of it, keep getting return of 2 instead of fff. need have implemented on bottom return duplicates. ?handling duplicates not trying prevent them inserting it, have return correct value.

        system.out.println("\ninsering duplicate key: ...");         tree.insert(2, "fff");         testfind(tree.find(2), 2, "fff"); 

i need handle duplicates , return "fff" above ^ test code bottom needs further implemented.

    public v find(k key) {         node node = findhelper(key, root);         if (node == null) {             return null;         } else {             return (v) node.entry.value;         }     }       public node findhelper(k key, node node) {         if (node.entry.key.compareto(key) == 0) {             return node;         }         else if (node.entry.key.compareto(key) > 0) {             if (node.leftchild == null) {                 return null;             } else {                 return findhelper(key, node.leftchild);             }         } else if (node.entry.key.compareto(key) < 0) {             if (node.rightchild == null) {                 return null;             } else {                 return findhelper(key, node.rightchild);             }         }         return node;     } 

if node entry key equals query, should continue searching in child nodes too:

public list<node> findhelper(k key, node node) {      int c = node.entry.key.compareto(key);      if (c == 0) {          list<node> result = new arraylist<>();          result.add(node);          result.addall(findhelper(key, node.leftchild));          result.addall(findhelper(key, node.rightchild));          return result;              } else if (c > 0) {         if (node.leftchild == null)              return collections.emptylist();         else              return findhelper(key, node.leftchild);      } else {         if (node.rightchild == null)              return collections.emptylist();         else              return findhelper(key, node.rightchild);     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -