Ejemplo n.º 1
0
        public static void levelOrder(BstNode root)
        {
            var q = new GenericDsQueue <BstNode>();

            if (root == null)
            {
                return;
            }

            q.Enqueue(root);
            while (!q.isEmpty())
            {
                var current = q.getFront();
                Console.Write(current.value.value + " ");
                if (current.value.left != null)
                {
                    q.Enqueue(current.value.left);
                }
                if (current.value.right != null)
                {
                    q.Enqueue(current.value.right);
                }
                q.Dequeue();
            }
        }
Ejemplo n.º 2
0
        protected override BstNode <T> LeftRightRotation(BstNode <T> node)
        {
            var newHead = base.LeftRightRotation(node);


            if (((AvlNode <T>)newHead).Balance < 0)
            {
                ((AvlNode <T>)node).Balance = 1;
                ((AvlNode <T>)newHead.LeftChild).Balance = 0;
            }
            else
            {
                if (((AvlNode <T>)newHead).Balance == 0)
                {
                    ((AvlNode <T>)node).Balance = 0;
                    ((AvlNode <T>)newHead.LeftChild).Balance = 0;
                }
                else
                {
                    ((AvlNode <T>)node).Balance = 0;
                    ((AvlNode <T>)newHead.LeftChild).Balance = -1;
                }
            }
            ((AvlNode <T>)newHead).Balance = 0;
            return(newHead);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            BstNode root = null;

            root = Insert(root, 2);
            root = Insert(root, -25);
            root = Insert(root, 10);
            root = Insert(root, 4);
            root = Insert(root, -3);
            root = Insert(root, 20);
            root = Insert(root, 21);
            root = Insert(root, 0);
            root = Insert(root, 20);
            root = Insert(root, 25);
            root = Insert(root, 8);
            root = Insert(root, 12);
            Console.WriteLine(Search(root, 5));
            Console.WriteLine(Search(root, 10));
            Console.WriteLine(FindMin(root));
            Console.WriteLine(FindMax(root));
            Console.WriteLine(height(root));
            Console.WriteLine();
            levelOrder(root);
            Console.WriteLine();
            PreOrder(root);
            Console.WriteLine();
            InOrder(root);
            Console.WriteLine();
            PostOrder(root);
            Console.WriteLine();
            Console.WriteLine(IsBST(root));
        }
Ejemplo n.º 4
0
        protected virtual BstNode <T> LeftRotation(BstNode <T> node)
        {
            var newHead = node.RightChild;
            var newHeadLeftRightChild = newHead.LeftChild;
            var newHeadParent         = node.Parent;

            newHead.Parent    = newHeadParent;
            node.Parent       = newHead;
            newHead.LeftChild = node;
            node.RightChild   = newHeadLeftRightChild;
            if (newHeadLeftRightChild != null)
            {
                newHeadLeftRightChild.Parent = node;
            }

            if (newHeadParent == null)
            {
                Root = newHead;
                return(newHead);
            }
            if (newHeadParent.RightChild == node)
            {
                newHeadParent.RightChild = newHead;
            }
            else
            {
                newHeadParent.LeftChild = newHead;
            }

            return(newHead);
        }
Ejemplo n.º 5
0
        private BstNode <T> FindNode(BstNode <T> node, T data)
        {
            var currentNode = node;

            while (currentNode != null)
            {
                if (data.CompareTo(currentNode.Data) == 0)
                {
                    return(currentNode);
                }

                if (data.CompareTo(currentNode.Data) < 0)
                {
                    currentNode = currentNode.LeftChild;
                    continue;
                }

                if (data.CompareTo(currentNode.Data) > 0)
                {
                    currentNode = currentNode.RightChild;
                    continue;
                }
            }

            return(null);
        }
Ejemplo n.º 6
0
        private bool FromLeft(BstNode <T> node)
        {
            if (node.HasOneChild && node.Parent.LeftChild == node.OnlyChild) //ak som mal jedno decko a parent ho ma teraz nalavo
            {
                return(true);
            }
            if (node.HasOneChild && node.Parent.RightChild == node.OnlyChild) //ak som mal jedno decko a parent ho ma teraz napravo
            {
                return(false);
            }
            //teraz uz mozem byt len list
            if (node.Parent.RightChild == null && node.Parent.LeftChild == null) //ak parent nema deti (novy list)
            {
                var n = (AvlNode <T>)node.Parent;
                if (n.Balance < 0) //balance sa mu nezmenili
                {
                    return(true);  //ak som ho tahal do lava
                }
                if (n.Balance > 0)
                {
                    return(false); //ak som ho tahal doprava
                }
            }
            //bol som list ale mal som brata
            if (node.Parent.RightChild != null) //ak pravy brat stale existuje tak som bol lavy
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 7
0
        public BstNode <T> Minimun()
        {
            BstNode <T> current = RootNode;

            while (current.LeftChild != null)
            {
                current = current.LeftChild;
            }
            return(current);
        }
Ejemplo n.º 8
0
 private void DoInOrder(BstNode <T> node, Processor processor) //act
 {
     if (node != null)
     {
         DoInOrder(node.LeftChild, processor);
         Console.WriteLine($"I am: {node.Data} and my parent is {(node.Parent != null ? node.Parent.Data: default(T))}");
         //processor(node.Data);
         DoInOrder(node.RightChild, processor);
     }
 }
Ejemplo n.º 9
0
        public BstNode <T> Maximum()
        {
            BstNode <T> current = RootNode;

            while (current.RightChild != null)
            {
                current = current.RightChild;
            }
            return(current);
        }
Ejemplo n.º 10
0
 public static int FindMax(BstNode root)
 {
     if (root.right == null)
     {
         return(root.value);
     }
     else
     {
         return(FindMax(root.right));
     }
 }
Ejemplo n.º 11
0
 private BstNode <T> RightMostValueFromLeftSubTree(BstNode <T> childNode)
 {
     while (true)
     {
         if (childNode.RightChild == null)
         {
             return(childNode);
         }
         childNode = childNode.RightChild;
     }
 }
Ejemplo n.º 12
0
 public static int FindMin(BstNode root)
 {
     if (root.left == null)
     {
         return(root.value);
     }
     else
     {
         return(FindMin(root.left));
     }
 }
Ejemplo n.º 13
0
 public static int height(BstNode root)
 {
     if (root == null)
     {
         return(-1);
     }
     if (root.left == null && root.right == null)
     {
         return(0);
     }
     return(Math.Max(height(root.left), height(root.right)) + 1);
 }
Ejemplo n.º 14
0
 static void PreOrder(BstNode root)
 {
     if (root != null)
     {
         Console.Write(root.value + " ");
     }
     if (root.left != null)
     {
         PreOrder(root.left);
     }
     if (root.right != null)
     {
         PreOrder(root.right);
     }
 }
Ejemplo n.º 15
0
        //adds a value to the tree.
        public void Insert(T data)
        {
            if (RootNode == null)
            {
                RootNode = new BstNode <T>(data);
            }
            else
            {
                BstNode <T> current = RootNode;
                bool        done    = false; //lets us exit the loop when a leaf is added.
                while (!done)
                {
                    //check left
                    if (data.CompareTo(current.Value) < 0)
                    {
                        if (current.LeftChild == null)
                        {
                            current.LeftChild = new BstNode <T>(data, current);
                            done = true;
                        }
                        current = current.LeftChild;
                    }
                    else //check right
                    {
                        if (current.RightChild == null)
                        {
                            current.RightChild = new BstNode <T>(data, current);
                            done = true;
                        }

                        current = current.RightChild;
                    }
                }
            }


            ////check if the tree has a root, if so, add to the tree,
            //if(RootNode != null)
            //{
            //    RootNode.Insert(data);
            //}
            //else
            //{
            //    //otherwise, the value becomes the root.
            //    Console.WriteLine($"Inserted {data}");
            //    RootNode = new BstNode<T>(data);
            //}
        }
Ejemplo n.º 16
0
 static void PostOrder(BstNode root)
 {
     if (root == null)
     {
         return;
     }
     if (root.left != null)
     {
         PostOrder(root.left);
     }
     if (root.right != null)
     {
         PostOrder(root.right);
     }
     Console.Write(root.value + " ");
 }
Ejemplo n.º 17
0
        protected BstNode <T> DeleteNode(BstNode <T> node, T data)
        {
            var nodeToDelete = FindNode(node, data);

            if (nodeToDelete != null)
            {
                if (nodeToDelete.LeftChild != null && nodeToDelete.RightChild != null)
                {
                    var rightMostNode = RightMostValueFromLeftSubTree(nodeToDelete.LeftChild);
                    var savedData     = nodeToDelete.Data;
                    nodeToDelete.Data  = rightMostNode.Data; //switch data
                    rightMostNode.Data = savedData;
                    nodeToDelete       = rightMostNode;
                }

                if (nodeToDelete.IsLeaf)
                {
                    nodeToDelete.RemoveMeFromParent();
                    if (nodeToDelete == Root)
                    {
                        Root = null;
                    }
                    return(nodeToDelete);
                }
                else if (nodeToDelete.HasOneChild)
                {
                    if (nodeToDelete == Root)
                    {
                        Root        = Root.OnlyChild;
                        Root.Parent = null;
                        return(nodeToDelete);
                    }
                    if (nodeToDelete.Parent.LeftChild == nodeToDelete)
                    {
                        nodeToDelete.Parent.LeftChild = nodeToDelete.OnlyChild;
                    }
                    else
                    {
                        nodeToDelete.Parent.RightChild = nodeToDelete.OnlyChild;
                    }

                    nodeToDelete.OnlyChild.Parent = nodeToDelete.Parent;
                    return(nodeToDelete);
                }
            }
            return(null);
        }
Ejemplo n.º 18
0
 static bool IsSubtreeGreater(BstNode root, int value)
 {
     if (root == null)
     {
         return(true);
     }
     if (root.value > value &&
         IsSubtreeGreater(root.left, value) &&
         IsSubtreeGreater(root.right, value)
         )
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 19
0
 static bool IsBST(BstNode root)
 {
     if (root == null)
     {
         return(true);
     }
     if (IsSubtreeLesser(root.left, root.value) &&
         IsSubtreeGreater(root.right, root.value) &&
         IsBST(root.left) &&
         IsBST(root.right))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 20
0
        static BstNode Insert(BstNode root, int value)
        {
            var newNode = new BstNode(value);

            if (root == null)
            {
                root = newNode;
            }
            else if (value <= root.value)
            {
                root.left = Insert(root.left, value);
            }
            else
            {
                root.right = Insert(root.right, value);
            }
            return(root);
        }
Ejemplo n.º 21
0
        protected override BstNode <T> RightRotation(BstNode <T> node)
        {
            var newHead = base.RightRotation(node);


            if (((AvlNode <T>)newHead).Balance == 0)
            {
                ((AvlNode <T>)node).Balance    = -1;
                ((AvlNode <T>)newHead).Balance = 1;
            }
            else
            {
                ((AvlNode <T>)node).Balance    = 0;
                ((AvlNode <T>)newHead).Balance = 0;
            }

            return(newHead);
        }
Ejemplo n.º 22
0
 static bool Search(BstNode root, int value)
 {
     if (root == null)
     {
         return(false);
     }
     else if (root.value == value)
     {
         return(true);
     }
     else if (root.value > value)
     {
         return(Search(root.left, value));
     }
     else
     {
         return(Search(root.right, value));
     }
 }
Ejemplo n.º 23
0
        public BstNode <T> Search(T data)
        {
            BstNode <T> current = RootNode;

            while (current != null)
            {
                if (data.CompareTo(current.Value) < 0)
                {
                    current = current.LeftChild;
                }
                else if (data.CompareTo(current.Value) > 0)
                {
                    current = current.RightChild;
                }
                else if (data.CompareTo(current.Value) == 0)
                {
                    return(current);
                }
            }

            return(null);
        }
Ejemplo n.º 24
0
        protected BstNode <T> InsertNode(BstNode <T> newNode, T data)
        {
            if (Root == null)
            {
                Root = newNode;
                return(Root);
            }
            var currentNode = Root;

            while (currentNode != null)
            {
                if (data.CompareTo(currentNode.Data) == 0)
                {
                    throw new Exception("Tree already contains such data");
                }

                if (data.CompareTo(currentNode.Data) < 0)
                {
                    if (currentNode.LeftChild == null)
                    {
                        currentNode.LeftChild = newNode;
                        newNode.Parent        = currentNode;
                        return(currentNode.LeftChild);
                    }
                    currentNode = currentNode.LeftChild;
                }
                else
                {
                    if (currentNode.RightChild == null)
                    {
                        currentNode.RightChild = newNode;
                        newNode.Parent         = currentNode;
                        return(currentNode.RightChild);
                    }
                    currentNode = currentNode.RightChild;
                }
            }
            return(null);
        }
Ejemplo n.º 25
0
 //constructor
 public BstNode(T value, BstNode <T> parent = null)
 {
     Value  = value;
     Parent = parent;
 }
Ejemplo n.º 26
0
 public BstNode(T data, BstNode <T> parent)
 {
     Data   = data;
     Parent = parent;
 }
Ejemplo n.º 27
0
 public BstNode(T data)
 {
     Data   = data;
     Parent = null;
 }
Ejemplo n.º 28
0
 public BST()
 {
     RootNode = null;
 }