예제 #1
0
        public SimpleBSTNode Ceil(SimpleBSTNode x, int key)
        {
            if (x == null)
            {
                return(null);
            }

            int cmp = key.CompareTo(x.key);

            if (cmp == 0)
            {
                return(x);
            }
            else if (cmp > 0)
            {
                return(Ceil(x.right, key));
            }
            else
            {   //reached a node whose key is more than input key
                //check if the right subtree of this node.
                //if floor in the right subtree is null, then this node's key is the floor
                SimpleBSTNode t = Ceil(x.left, key);
                if (t != null)
                {
                    return(t);
                }
                else
                {
                    return(x);
                }
            }
        }
예제 #2
0
        //Hibbard deletion : replace node to be deleted with smallest node in right subtree and make appropriate adjustments
        //Problem with Hibbard deletion : it is assymetric. After a series of insertions and deletions with this technique,
        //the height of the tree becomes sqrt(N) as opposed to log(N). It might make a difference b/w acceptable and unacceptable
        //solution in real world applications. Simple and efficient deletion for BSTs is a long standing open problem. - Robert Sedgewick

        private SimpleBSTNode Delete(SimpleBSTNode x, int key)
        {
            if (x == null)
            {
                return(null);
            }

            int cmp = key.CompareTo(x.key);

            if (cmp < 0)
            {
                x.left = Delete(x.left, key);
            }
            else if (cmp > 0)
            {
                x.right = Delete(x.right, key);
            }
            else
            {//cmp==0 => x is the node to be deleted
                if (x.right == null)
                {
                    return(x.left);
                }

                SimpleBSTNode t = x;
                x       = Min(t.right);
                x.right = DeleteMin(t.right);
                x.left  = t.left;
            }
            x.count = 1 + Size(x.left) + Size(x.right);
            return(x);
        }
예제 #3
0
 private SimpleBSTNode Put(SimpleBSTNode x, int key, int val)
 {
     if (x == null)
     {
         x = new SimpleBSTNode(key, val);
     }
     else
     {
         int cmp = key.CompareTo(x.key);
         if (cmp < 0)
         {
             x.left = Put(x.left, key, val);
         }
         if (cmp > 0)
         {
             x.right = Put(x.right, key, val);
         }
         if (cmp == 0)
         {
             x.val = val;
         }
     }
     x.count = 1 + Size(x.left) + Size(x.right);
     return(x);
 }
예제 #4
0
 public SimpleBSTNode Min(SimpleBSTNode x)
 {
     while ((x != null) && (x.left != null))
     {
         x = x.left;
     }
     return(x);
 }
예제 #5
0
 public int Max(SimpleBSTNode x)
 {
     while ((x != null) && (x.right != null))
     {
         x = x.right;
     }
     return(x.key);
 }
예제 #6
0
 public SimpleBSTNode DeleteMin(SimpleBSTNode x)
 {
     if (x.left == null)
         return x.right;
     x.left = DeleteMin(x.left);
     x.count = 1 + Size(x.left) + Size(x.right);
     return x;
 }
예제 #7
0
 public int Size(SimpleBSTNode x)
 {
     if (x == null)
     {
         return(0);
     }
     return(x.count);
 }
예제 #8
0
 private void Inorder(SimpleBSTNode x, Queue q)
 {
     if (x == null)
     {
         return;
     }
     Inorder(x.left, q);
     q.Enqueue(x);
     Inorder(x.right, q);
 }
예제 #9
0
 public SimpleBSTNode DeleteMin(SimpleBSTNode x)
 {
     if (x.left == null)
     {
         return(x.right);
     }
     x.left  = DeleteMin(x.left);
     x.count = 1 + Size(x.left) + Size(x.right);
     return(x);
 }
예제 #10
0
        public int Rank(SimpleBSTNode x, int key)
        {
            if (x == null)
            {
                return(0);
            }
            int cmp = key.CompareTo(x.key);

            if (cmp == 0)
            {
                return(Size(x.left));
            }
            else if (cmp < 0)
            {
                return(Rank(x.left, key));
            }
            else
            {
                return(1 + Size(x.left) + Rank(x.right, key));
            }
        }
예제 #11
0
        public int Get(int key)
        {
            SimpleBSTNode x = root;

            while (x != null)
            {
                int cmp = key.CompareTo(x.key);
                if (cmp == 0)
                {
                    return(x.val);
                }
                if (cmp < 0)
                {
                    x = x.left;
                }
                if (cmp > 0)
                {
                    x = x.right;
                }
            }
            return(-1); //null
        }
예제 #12
0
        public SimpleBSTNode Floor(SimpleBSTNode x, int key)
        {
            if (x == null)
                return null;

            int cmp = key.CompareTo(x.key);

            if (cmp == 0)
                return x;
            else if (cmp < 0)
                return Floor(x.left, key);
            else
            {   //reached a node whose key is less than input key
                //check if the right subtree of this node.
                //if floor in the right subtree is null, then this node's key is the floor
                SimpleBSTNode t = Floor(x.right, key);
                if (t != null)
                    return t;
                else
                    return x;
            }
        }
예제 #13
0
 private void RangeSelect(SimpleBSTNode x, int lo, int hi, Queue q)
 {
     if (x == null)
     {
         return;
     }
     if (x.key.CompareTo(lo) < 0) //lower bound of interval more than current node
     {
         RangeSelect(x.right, lo, hi, q);
     }
     else if (x.key.CompareTo(hi) > 0) //upper bound of interval less than current node
     {
         RangeSelect(x.left, lo, hi, q);
     }
     else
     {
         //Inorder(x, q);
         RangeSelect(x.left, lo, hi, q);
         q.Enqueue(x);
         RangeSelect(x.right, lo, hi, q);
     }
 }
예제 #14
0
 public int Max(SimpleBSTNode x)
 {
     while ((x != null) && (x.right != null))
         x = x.right;
     return x.key;
 }
예제 #15
0
 public void Put(int key, int val)
 {
     root = Put(root, key, val);
 }
예제 #16
0
 public void Put(int key, int val)
 {
     root = Put(root, key, val);
 }
예제 #17
0
        public int count;  //count of nodes in subtree rooted at current node

        public SimpleBSTNode(int key, int val)
        {
            this.key = key; this.val = val;
            //left = null; right = null;
            left = right = null;
        }
예제 #18
0
 public int Rank(SimpleBSTNode x, int key)
 {
     if (x == null)
         return 0;
     int cmp = key.CompareTo(x.key);
     if (cmp == 0)
         return Size(x.left);
     else if (cmp < 0)
         return Rank(x.left, key);
     else
     {
         return 1 + Size(x.left) + Rank(x.right, key);
     }
 }
예제 #19
0
 public int Size(SimpleBSTNode x)
 {
     if (x == null)
         return 0;
     return x.count;
 }
예제 #20
0
        //Hibbard deletion : replace node to be deleted with smallest node in right subtree and make appropriate adjustments
        //Problem with Hibbard deletion : it is assymetric. After a series of insertions and deletions with this technique,
        //the height of the tree becomes sqrt(N) as opposed to log(N). It might make a difference b/w acceptable and unacceptable
        //solution in real world applications. Simple and efficient deletion for BSTs is a long standing open problem. - Robert Sedgewick
        private SimpleBSTNode Delete(SimpleBSTNode x, int key)
        {
            if (x == null)
                return null;

            int cmp = key.CompareTo(x.key);

            if (cmp < 0)
                x.left = Delete(x.left, key);
            else if (cmp > 0)
                x.right = Delete(x.right, key);
            else
            {//cmp==0 => x is the node to be deleted

                if (x.right == null)
                    return x.left;

                SimpleBSTNode t = x;
                x = Min(t.right);
                x.right = DeleteMin(t.right);
                x.left = t.left;

            }
            x.count = 1 + Size(x.left) + Size(x.right);
            return x;
        }
예제 #21
0
 private void Inorder(SimpleBSTNode x,Queue q)
 {
     if (x == null)
         return;
     Inorder(x.left, q);
     q.Enqueue(x);
     Inorder(x.right, q);
 }
예제 #22
0
 public void Delete(int key)
 {
     root = Delete(root, key);
 }
예제 #23
0
 private SimpleBSTNode Put(SimpleBSTNode x, int key, int val)
 {
     if (x == null)
     {
         x = new SimpleBSTNode(key, val);
     }
     else
     {
         int cmp = key.CompareTo(x.key);
         if (cmp < 0)
             x.left = Put(x.left, key, val);
         if (cmp > 0)
             x.right = Put(x.right, key, val);
         if (cmp == 0)
             x.val = val;
     }
     x.count = 1 + Size(x.left) + Size(x.right);
     return x;
 }
예제 #24
0
 private void RangeSelect(SimpleBSTNode x, int lo, int hi, Queue q)
 {
     if (x == null)
         return;
     if (x.key.CompareTo(lo) < 0) //lower bound of interval more than current node
         RangeSelect(x.right, lo, hi,q);
     else if (x.key.CompareTo(hi) > 0) //upper bound of interval less than current node
         RangeSelect(x.left, lo, hi,q);
     else
     {
         //Inorder(x, q);
         RangeSelect(x.left, lo, hi, q);
         q.Enqueue(x);
         RangeSelect(x.right, lo, hi, q);
     }
 }
예제 #25
0
 public SimpleBSTNode(int key, int val)
 {
     this.key = key; this.val = val;
     //left = null; right = null;
     left = right = null;
 }
예제 #26
0
 public SimpleBSTNode Min(SimpleBSTNode x)
 {
     while ((x != null)&&(x.left!=null))
         x = x.left;
     return x;
 }
예제 #27
0
 public void Delete(int key)
 {
     root = Delete(root, key);
 }