示例#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();
            }
        }
示例#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);
        }
示例#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));
        }
示例#4
0
文件: BSTree.cs 项目: Juraj04/Trees
        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);
        }
示例#5
0
文件: BSTree.cs 项目: Juraj04/Trees
        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);
        }
示例#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);
        }
示例#7
0
        public BstNode <T> Minimun()
        {
            BstNode <T> current = RootNode;

            while (current.LeftChild != null)
            {
                current = current.LeftChild;
            }
            return(current);
        }
示例#8
0
文件: BSTree.cs 项目: Juraj04/Trees
 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);
     }
 }
示例#9
0
        public BstNode <T> Maximum()
        {
            BstNode <T> current = RootNode;

            while (current.RightChild != null)
            {
                current = current.RightChild;
            }
            return(current);
        }
示例#10
0
 public static int FindMax(BstNode root)
 {
     if (root.right == null)
     {
         return(root.value);
     }
     else
     {
         return(FindMax(root.right));
     }
 }
示例#11
0
文件: BSTree.cs 项目: Juraj04/Trees
 private BstNode <T> RightMostValueFromLeftSubTree(BstNode <T> childNode)
 {
     while (true)
     {
         if (childNode.RightChild == null)
         {
             return(childNode);
         }
         childNode = childNode.RightChild;
     }
 }
示例#12
0
 public static int FindMin(BstNode root)
 {
     if (root.left == null)
     {
         return(root.value);
     }
     else
     {
         return(FindMin(root.left));
     }
 }
示例#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);
 }
示例#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);
     }
 }
示例#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);
            //}
        }
示例#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 + " ");
 }
示例#17
0
文件: BSTree.cs 项目: Juraj04/Trees
        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);
        }
示例#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);
     }
 }
示例#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);
     }
 }
示例#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);
        }
示例#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);
        }
示例#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));
     }
 }
示例#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);
        }
示例#24
0
文件: BSTree.cs 项目: Juraj04/Trees
        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);
        }
示例#25
0
 //constructor
 public BstNode(T value, BstNode <T> parent = null)
 {
     Value  = value;
     Parent = parent;
 }
示例#26
0
文件: BSTree.cs 项目: Juraj04/Trees
 public BstNode(T data, BstNode <T> parent)
 {
     Data   = data;
     Parent = parent;
 }
示例#27
0
文件: BSTree.cs 项目: Juraj04/Trees
 public BstNode(T data)
 {
     Data   = data;
     Parent = null;
 }
示例#28
0
 public BST()
 {
     RootNode = null;
 }