Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.94 KB | None | 0 0
  1. public class BST {
  2.     /* Root of BST */
  3.     private Node root;
  4.     /* Number of nodes in BST */
  5.     private int size;
  6.  
  7.     public BST() {
  8.         this.root = null;
  9.         this.size = 0;
  10.     }
  11.  
  12.     /**
  13.      * Is the BST empty?
  14.      */
  15.     public boolean isEmpty() {
  16.         return size() == 0;
  17.     }
  18.  
  19.     /**
  20.      * Return root of BST
  21.      */
  22.     public Node getRoot() {
  23.         return root;
  24.     }
  25.  
  26.     /**
  27.      * Return number of key-value pairs in BST
  28.      */
  29.     public int size() {
  30.         return size;
  31.     }
  32.  
  33.     /**
  34.      * Does there exist a key-value pair with given key?
  35.      */
  36.     public boolean contains(int key) {
  37.         return find(key) != null;
  38.     }
  39.  
  40.     /**
  41.      * Return value associated with the given key, or null if no such key exists
  42.      */
  43.     public String find(int key) {
  44.         return find(root, key);
  45.     }
  46.  
  47.     /**
  48.      * Return value associated with the given key, or null if no such key exists
  49.      * in subtree rooted at x
  50.      */
  51.     private String find(Node x, int key) {
  52.         if (x == null) {
  53.             return null;
  54.         }
  55.         if (key < x.key) {
  56.             return find(x.left, key);
  57.         } else if (key > x.key) {
  58.             return find(x.right, key);
  59.         } else {
  60.             return x.val;
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * Insert key-value pair into BST. If key already exists, update with new
  66.      * value
  67.      */
  68.     public void insert(int key, String val) {
  69.         if (val == null) {
  70.             remove(key);
  71.             return;
  72.         }
  73.         root = insert(root, key, val);
  74.     }
  75.  
  76.     /**
  77.      * Insert key-value pair into subtree rooted at x. If key already exists,
  78.      * update with new value.
  79.      */
  80.     private Node insert(Node x, int key, String val) {
  81.         if (x == null) {
  82.             size++;
  83.             return new Node(key, val);
  84.         }
  85.         if (key < x.key) {
  86.             x.left = insert(x.left, key, val);
  87.         } else if (key > x.key) {
  88.             x.right = insert(x.right, key, val);
  89.         } else {
  90.             x.val = val;
  91.         }
  92.  
  93.         return x;
  94.     }
  95.  
  96.     /**
  97.      * Remove node with given key from BST
  98.      */
  99.     public void remove(int key) {
  100.         remove(root,key);
  101.  
  102.  
  103.  
  104.     } // dummy code
  105.  
  106.     public Node remove (Node x, int key) {
  107.  
  108.         if (x== null) {
  109.             return null;
  110.         }
  111.        
  112.         if (key < x.key) {
  113.             x.left = remove(x.left, key);
  114.  
  115.         } else if (key > x.key) {
  116.             x.right = remove(x.right, key);
  117.  
  118.         } else {
  119.            
  120.             if (x.left == null) {
  121.                 return x.right;
  122.             }else if (x.right == null) {
  123.                 return x.left;
  124.             }else {
  125.  
  126.                 Node tmp = getMax(x.left,key);
  127.                 x.key = tmp.key;               
  128.                 x.left = findMax(x.left,key);
  129.                
  130.             }
  131.             if(root.left == null && root.key == key){
  132.                 Node tmpRoot = new Node (key, null);
  133.                 tmpRoot = root.right;
  134.                 root = tmpRoot;
  135.                 tmpRoot= null;    //HÄR ÄR DET TOK
  136.             }
  137.  
  138.         }
  139.         return x;
  140.     }
  141.  
  142.     private Node getMax (Node x, int key) {
  143.         if (x.right == null) {
  144.             return x;
  145.         }
  146.         return getMax(x.right, key);
  147.     }
  148.  
  149.  
  150.     private Node findMax (Node x, int key) {
  151.  
  152.         if (x.right ==null ) {
  153.             return x.left;
  154.         }
  155.  
  156.         x.right = findMax(x.right, key);
  157.         return x;
  158.     }
  159.  
  160.  
  161.     public PreorderIterator preorder() {
  162.         return new PreorderIterator(root);
  163.     }
  164.  
  165.     public LevelorderIterator levelorder() {
  166.         return new LevelorderIterator(root);
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement