Exemple #1
0
        /*
         * Key insertion operation
         * Complexity: O(n)
         */
        public static BST <T> Insert(BST <T> _tree, T _value)
        {
            // Check if left or right child should be used
            var comparison = _value.CompareTo(_tree.Value);

            if (comparison < 0)
            {
                if (_tree.Left == null)
                {
                    Console.WriteLine("Inserting {0} left of {1}",
                                      _value, _tree.Value);
                    _tree.Left = new BST <T>(_value);
                }
                else
                {
                    Console.WriteLine("Moving left of {0}",
                                      _tree.Value);
                    _tree.Left = Insert(_tree.Left, _value);
                }
            }
            else
            {
                if (_tree.Right == null)
                {
                    Console.WriteLine("Inserting {0} right of {1}",
                                      _value, _tree.Value);
                    _tree.Right = new BST <T>(_value);
                }
                else
                {
                    Console.WriteLine("Moving right of {0}",
                                      _tree.Value);
                    _tree.Right = Insert(_tree.Right, _value);
                }
            }

            return(_tree);
        }
Exemple #2
0
        /*
         * Delete operation
         * Complexity: O(n^2) because of the added cost of the successor-locating traversal
         */
        public static BST <T> Delete(BST <T> _tree, T _value, BST <T> _root)
        {
            if (_tree == null)
            {
                Console.WriteLine("Value not found in tree");
            }
            else
            {
                var comparison = _value.CompareTo(_tree.Value);
                if (comparison == 0)
                {
                    if (_tree.Left == null && _tree.Right == null)
                    {
                        Console.WriteLine("Removing leaf {0}", _tree.Value);
                        _tree = null;
                    }
                    else if (_tree.Left != null && _tree.Right != null)
                    {
                        // Locate successor, copy and recursively delete
                        var traversal = TraverseInOrder(_root);
                        for (int i = 0; i < traversal.Count; i++)
                        {
                            if (_tree.Value.CompareTo(traversal[i].Value) == 0)
                            {
                                var successor = traversal[i + 1];
                                Console.WriteLine("Replacing {0} with successor {1}",
                                                  _tree.Value, successor.Value);
                                _tree.Value = successor.Value;
                                Console.WriteLine("Recursively calling delete on {0}",
                                                  _tree.Right.Value);
                                _tree.Right = Delete(_tree.Right, successor.Value, _root);
                                break;
                            }
                        }
                    }
                    else if (_tree.Left != null)
                    {
                        Console.WriteLine("Replacing {0} with right subtree {1}",
                                          _tree.Value, _tree.Left.Value);
                        _tree = _tree.Left;
                    }
                    else
                    {
                        Console.WriteLine("Replacing {0} with right subtree {1}",
                                          _tree.Value, _tree.Right.Value);
                        _tree = _tree.Right;
                    }
                }
                else if (comparison < 0)
                {
                    Console.WriteLine("Moving left from {0}", _tree.Value);
                    _tree.Left = Delete(_tree.Left, _value, _root);
                }
                else
                {
                    Console.WriteLine("Moving right from {0}", _tree.Value);
                    _tree.Right = Delete(_tree.Right, _value, _root);
                }
            }

            return(_tree);
        }
Exemple #3
0
 static bool Search <T>(BST <T> _tree, T _which) where T : IComparable
 {
     Console.WriteLine("Searching for {0}", _which.ToString());
     return(BST <T> .Search(_tree, _which));
 }