algorithm - How to reverse (mirror) a recursive binary search tree's subtrees -


i trying write binary search tree in java. bst takes number of "keywords" , inserts them tree using recursive methods.

unfortunately, seems adding them backwards, e.x. right side higher letters (a-c...) left side (x-z...).

i cannot figure out how correctly reverse logic.

here insertion code:

  /**  * method creates new record thefiledata.  * recursive insertion method, adds recordtoadd list of records  * node associated thekeyword.  *   * if there no keyword, create new node it.  *   * @param thekeyword keyword associate new record.  * @param thefiledata file data associate new record.  */ public void insert(string thekeyword, filedata fd) {     if (fd == null) {         throw new nullpointerexception("invalid file data.");     }     if (thekeyword == null) {         throw new nullpointerexception("invalid keyword.");     }     thekeyword = thekeyword.tolowercase();      record recordtoadd = new record(fd.id, fd.author, fd.title, null);      // step 1 find node keyword thekeyword. give correct list insert into.     if (root == null) {         /*          * if tree empty, create new node root.          * node has record added it's records list.          */         node newnode = new node(thekeyword);         newnode.update(recordtoadd);         root = newnode;     } else if (!contains(thekeyword)) {         node newnode = new node(thekeyword);         newnode.update(recordtoadd);         insert(root, newnode);     } else {         node target = find(thekeyword, root);         target.update(recordtoadd);     } }  /**  * recursive insertion helper method allows , add new node object  * our bst.  */ private node insert(node theparent, node thenode) {     if (theparent == null) {         return thenode;     } else if (thenode.keyword.compareto(theparent.keyword) < 0) {         theparent.right = insert(theparent.right, thenode);     } else if (thenode.keyword.compareto(theparent.keyword) > 0) {         theparent.left = insert(theparent.left, thenode);     }     return theparent; }  /**  * helper method searches given keyword, returning node when found.  *   * @return node containing keyword looking for. else null.  */ private node find(string keyword, node root) {     if (keyword == null) {         throw new illegalargumentexception("invalid keyword.");     }      if (root == null) {         return null;     }     keyword = keyword.tolowercase();     if (keyword.compareto(root.keyword) > 0) {         return find(keyword, root.left);     }     if (keyword.compareto(root.keyword) < 0) {         return find(keyword, root.right);     }     return root; }  /**  * method calls find helper method. if find returns null, know value not exist.  *   * @param keyword keyword search for.  * @return true or false depending on if keyword exists in bst.  */ public boolean contains(string keyword) {      keyword = keyword.tolowercase();      if (find(keyword, root) != null) {         return true; // if keyword exists.     }     return false; } 

strong text

here graphic representation of tree:

| | |-------blobs
| |-------buildings
| | | |-------causal-relationships
| | | | |-------classification-rules
| | |-------clustering
|-------content-based
| |-------data-mining
database
| | |-------distance-measures
| | | |-------image-display
| |-------image-management
|-------image-retrieval
| | | |-------image-stack
| | | | | |-------indexing
| | | | | | | |-------information-retrieval
| | | | | | |-------instance-based
| | | | | | | |-------instance-based
| | | | |-------knowledge
| | |-------lines
| |-------matching
| | | | | |-------multimedia
| | | | | | |-------neural-networks
| | | | |-------pose
| | | |-------pruning
| | | | |-------queries
| | |-------query-by-example
| | | | |-------query-trees
| | | |-------recognition
| | | | | |-------region-based
| | | | | | |-------relational
| | | | |-------search
| | | | | |-------similarity
| | | | | | | |-------spatial
| | | | | | | | |-------temporal
| | | | | | | | | |-------time-related
| | | | | | |-------triangle-inequality
| | | | | | | |-------weighting

blobs should on left, matching should on right, etc.

in code, reverse < , >. code reads

private node insert(node theparent, node thenode) { if (theparent == null) {     return thenode; } else if (thenode.keyword.compareto(theparent.keyword) > 0) {     theparent.right = insert(theparent.right, thenode); } else if (thenode.keyword.compareto(theparent.keyword) < 0) {     theparent.left = insert(theparent.left, thenode); }     return theparent; } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -