Exemplo n.º 1
0
        private IEnumerable <T> PreOrderTraversalHelper(
            MyBinarySearchTreeNode <T> root)
        {
            if (root == null)
            {
                yield break;
            }

            yield return(root.Data);

            List <T> data = new List <T>();

            if (root.Left != null)
            {
                data.AddRange(PreOrderTraversalHelper(root.Left));
            }
            if (root.Right != null)
            {
                data.AddRange(PreOrderTraversalHelper(root.Right));
            }

            foreach (var item in data)
            {
                yield return(item);
            }
        }
Exemplo n.º 2
0
        private MyBinarySearchTreeNode <T> FindSmallestNodeInRightSubTree(
            MyBinarySearchTreeNode <T> root)
        {
            if (root == null)
            {
                return(root);
            }
            var node = FindSmallestNodeInRightSubTree(root.Left);

            return(node ?? root);
        }
Exemplo n.º 3
0
        private MyBinarySearchTreeNode <T> FindLargestNodeInLeftSubStree(MyBinarySearchTreeNode <T> root)
        {
            if (root == null)
            {
                return(root);
            }

            var node = FindLargestNodeInLeftSubStree(root.Right);

            return(node ?? root);
        }
Exemplo n.º 4
0
        public virtual void Insert(T data)
        {
            if (Root == null)
            {
                Root = new MyBinarySearchTreeNode <T>(data);
            }
            else
            {
                InsertHelper(data, Root);
            }

            Count++;
        }
Exemplo n.º 5
0
        private void InsertHelper(T data, MyBinarySearchTreeNode <T> root)
        {
            var compared = data.CompareTo(root.Data);

            if (compared == 0)
            {
                var node = new MyBinarySearchTreeNode <T>(data);

                if (root.Left != null)
                {
                    node.Left  = root.Left;
                    node.Right = root.Left.Right;
                    root.Left  = node;
                }
                else
                {
                    root.Left = node;
                }
            }
            if (compared < 0)
            {
                if (root.Left == null)
                {
                    root.Left = new MyBinarySearchTreeNode <T>(data);
                }
                else
                {
                    InsertHelper(data, root.Left);
                }
            }
            if (compared > 0)
            {
                if (root.Right == null)
                {
                    root.Right = new MyBinarySearchTreeNode <T>(data);
                }
                else
                {
                    InsertHelper(data, root.Right);
                }
            }
        }
Exemplo n.º 6
0
        private bool ContainsHelper(MyBinarySearchTreeNode <T> node,
                                    T data)
        {
            if (node == null)
            {
                return(false);
            }
            if (node.Data.Equals(data))
            {
                return(true);
            }

            if (ContainsHelper(node.Left, data))
            {
                return(true);
            }
            if (ContainsHelper(node.Right, data))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
 public MyBinarySearchTreeNode(T data, MyBinarySearchTreeNode <T> left,
                               MyBinarySearchTreeNode <T> right) : this(data)
 {
     Left  = left;
     Right = right;
 }
Exemplo n.º 8
0
        private bool RemoveHelper(MyBinarySearchTreeNode <T> root,
                                  MyBinarySearchTreeNode <T> parent, T data)
        {
            if (root == null)
            {
                return(false);
            }

            if (root.Data.Equals(data))
            {
                if (root.Left == null && root.Right == null)
                {
                    if (parent.Left != null && parent.Left.Data.Equals(root.Data))
                    {
                        parent.Left = null;
                    }
                    else
                    {
                        parent.Right = null;
                    }
                    return(true);
                }
                if (root.Left != null && root.Right == null)
                {
                    if (parent.Right != null && parent.Right.Data.Equals(root.Data))
                    {
                        parent.Right = root.Left;
                    }
                    else
                    {
                        parent.Left = root.Left;
                    }
                    return(true);
                }
                if (root.Right != null && root.Left == null)
                {
                    if (parent.Right != null && parent.Right.Data.Equals(root.Data))
                    {
                        parent.Right = root.Right;
                    }
                    else
                    {
                        parent.Left = root.Right;
                    }
                    return(true);
                }
                if (root.Right != null && root.Left != null)
                {
                    var swapNode = FindLargestNodeInLeftSubStree(root.Left) ??
                                   FindSmallestNodeInRightSubTree(root.Right);

                    var temp = swapNode.Data;
                    swapNode.Data = root.Data;
                    root.Data     = temp;

                    return(RemoveHelper(Root, null, swapNode.Data));
                }
            }

            if (RemoveHelper(root.Left, root, data))
            {
                return(true);
            }
            if (RemoveHelper(root.Right, root, data))
            {
                return(true);
            }

            return(false);
        }