java - Creating a binary search tree-linked list hybrid -


i'm trying write structure functions bst , linked list.

the idea structure used tree when searching or inserting data, printing data use structure list.

i've gotten solution work (i.e. inorder traversal produces same output walking list), unless try insert duplicate data. i'm wondering if there's better way this, or if should done!

current implementation:

package com.practice.structures;  public class binarysearchlisttree {     treelistnode root;     treelistnode head;     public binarysearchlisttree() {     }      public void add(int data) {         treelistnode node = new treelistnode(data);         if(null == this.root) {             this.root = node;             this.head = node;             return;         }          insertnode(node, this.root, this.root);     }      private void insertnode(treelistnode node, treelistnode parent, treelistnode hold) {        if(parent.getdata() >= node.getdata()) {            if(null == parent.leftchild) {                addleftchild(node, parent, hold);            } else {                if(parent.leftchild.getdata() >= hold.getdata()) {                    insertnode(node, parent.leftchild, hold);                } else {                    insertnode(node, parent.leftchild, parent.leftchild);                }            }        } else {            if(null == parent.rightchild) {                addrightchild(node, parent);            } else {                insertnode(node, parent.rightchild, parent);            }        }     }      public void traverse() {         inordertraverse(this.root);         system.out.println();     }      public void printlist() {         printnode(this.head);         system.out.println();     }      private void printnode(treelistnode node) {         if(null != node) {             system.out.print(node.getdata() + " ");             printnode(node.next);         }     }      private void inordertraverse(treelistnode node) {         if(node == null) {             return;         }         inordertraverse(node.leftchild);         system.out.print(node.getdata() + " ");         inordertraverse(node.rightchild);     }      private void addleftchild(treelistnode child, treelistnode parent, treelistnode hold) {         parent.addleftchild(child);         child.next = parent;         if(parent != hold) {             hold.next = child;         }         if(parent == this.head) {             this.head = child;         }     }      private void addrightchild(treelistnode child, treelistnode parent) {         parent.addrightchild(child);         child.next = parent.next;         parent.next = child;     } } 


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -