Exemplo n.º 1
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            _tree = new BinaryTree();
            //lblEvents.Text = @"new binary tree";
            Node root = new Node(25);
            _tree.Add(root);

            _tree.Add(new Node(20));

            _tree.Add(new Node(30));

            _tree.Add(new Node(10));
            _tree.Add(new Node(23));

            _tree.Add(new Node(35));

            _tree.Add(new Node(5));
            _tree.Add(new Node(4));
            _tree.Add(new Node(6));

            _tree.Add(new Node(21));
            _tree.Add(new Node(19));
            _tree.Add(new Node(24));

            _tree.Add(new Node(32));
            _tree.Add(new Node(50));
            _tree.Add(new Node(33));

            PaintTree();
        }
Exemplo n.º 2
0
 //DLR traverse tree
 static BiTree<string> CreateBiTree()
 {
     Node<string> a = new Node<string>("A");
     Node<string> b = new Node<string>("B");
     Node<string> c = new Node<string>("C");
     Node<string> e = new Node<string>("E");
     Node<string> d = new Node<string>("D");
     Node<string> f = new Node<string>("F");
     Node<string> g = new Node<string>("G");
     Node<string> h = new Node<string>("H");
     Node<string> i = new Node<string>("I");
     Node<string> j = new Node<string>("J");
     a.LChild = b;
     a.RChild = c;
     b.LChild = d;
     b.RChild = e;
     c.LChild = f;
     c.RChild = g;
     d.LChild = h;
     d.RChild = i;
     e.LChild = j;
     BiTree<string> bTree =new BiTree<string>(a);
     //Console.wr
     return bTree;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Delete a node by key.  This will only delete one occurence of a 
        /// node with that key.
        /// </summary>
        /// <param name="val">Key of node to delete</param>
        public void Delete(int val)
        {
            Node curr = Find(val);
            Node parent = FindParent(root, val);
            Node node = null, nodeParent = null, nodeChild = null;
            if (curr.Left == null || curr.Right == null)
                node = curr;
            else
                node = FindSuccessor(curr);

            nodeParent = FindParent(root, node.Val);

            if (node.Left != null)
                nodeChild = node.Left;
            else
                nodeChild = node.Right;

            if (nodeParent == null) // root
            {
                root = nodeChild;
            }
            else
            {
                if (node == nodeParent.Left)
                    nodeParent.Left = nodeChild;
                else
                    nodeParent.Right = nodeChild;
            }
            if (node != curr)
                curr.Val = node.Val;
        }
Exemplo n.º 4
0
 public void buildTree()
 {
     int[] elements = { 5, 4, 6, 2, 3, 8, 1, 9, 10, 7 };
     foreach (int data in elements)
     {
         root = insert(root, data);
     }
 }
Exemplo n.º 5
0
 public bool IsMirror(Node a, Node b)
 {
     if (a == null && b == null)
         return true;
     if (a != null && b != null)
         return (a.data == b.data) && IsMirror(a.left, b.right) && IsMirror(a.right, b.left);
     else
         return false;
 }
Exemplo n.º 6
0
        public void inorder(Node node)
        {
            if (node == null)
                return;

            inorder(node.left);
            Console.Write(" {0} ", node.data);
            inorder(node.right);
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            Node n1 = new Node(1, null, null);
            Node n3 = new Node(3, null, null);
            Node n2 = new Node(2, n1, n3);

            Console.WriteLine(IsValidBST(n2));

            Console.ReadLine();
        }
Exemplo n.º 8
0
 static void PostOrder(Node<string> root)
 {
     if (root == null)
     {
         return;
     }
     PostOrder(root.LChild);
     PostOrder(root.RChild);
     Console.Write(root.Data + "   ");
 }
Exemplo n.º 9
0
        public void ConvertToMirror(Node node)
        {
            if (node == null)
                return;

            ConvertToMirror(node.left);
            ConvertToMirror(node.right);

            Node temp = node.left;
            node.left = node.right;
            node.right = temp;
        }
Exemplo n.º 10
0
        static void PreOrder(Node<string> root)
        {
            if (root == null)
            {
                return;
            }
            Console.Write(root.Data + "   ");

            PreOrder(root.LChild);//traverse left tree

            PreOrder(root.RChild);//traverse right tree
        }
Exemplo n.º 11
0
        static bool Check(Node node, Stack<Node> stack)
        {
            if (node == null)
                return true;

            if (Check(node.Left, stack) && (stack.Count == 0 || node.Value > stack.Peek().Value))
                stack.Push(node);
            else
                return false;

            return Check(node.Right, stack);
        }
Exemplo n.º 12
0
 private Node insert(Node node, int data)
 {
     if (node == null)
         return new Node(data);
     else
     {
         if (node.data >= data)
             node.left = insert(node.left, data);
         else
             node.right = insert(node.right, data);
         return node;
     }
 }
Exemplo n.º 13
0
        public static Node InorderAndPreorderToTree(int[] pre, int[] inorder, int instrt, int inend)
        {
            if (instrt > inend)
                return null;

            Node node = new Node(pre[preIndex++]);

            if (instrt == inend)
                return node;

            int inIndex;
            for (inIndex = instrt; inIndex <= inend; inIndex++)
            {
                if (inorder[inIndex] == node.data)
                    break;
            }

            node.left = InorderAndPreorderToTree(pre, inorder, instrt, inIndex - 1);
            node.right = InorderAndPreorderToTree(pre, inorder, inIndex + 1, inend);
            return node;
        }
Exemplo n.º 14
0
        private void btnRnd_Click(object sender, EventArgs e)
        {
            var val = int.Parse(textBox1.Text);
            var val2 = int.Parse(textBox2.Text);

            Node n1 = new Node(val);
            Node n2 = new Node(val2);

              BinaryTree i = new BinaryTree();

              Node root = i.RootNode;

             if (_tree.Exists(val) && _tree.Exists(val2))
             {

              MessageBox.Show("The parent node is " + i.LCA(root, n1, n2).Value, "Please Hire Me!");
             }
             else
             {
                MessageBox.Show("Node values are invalid please try again","Error");
             }
        }
Exemplo n.º 15
0
            public void insert(int id, double data)
            {
                Node newnode = new Node(); // creating a new node
                newnode.iData = id;
                newnode.fData = data;

                if (root == null)
                    root = newnode;
                else
                {
                    Node current = root;
                    Node parent;
                    while (true)
                    {
                        parent = current;

                        if (id < current.iData)  // go left
                        {
                            current = current.leftChild;
                            if (current == null)
                            {
                                parent.leftChild = newnode;
                                return;
                            }
                        } // end of the the left hand
                        else
                        {
                            current = current.rightChild;
                            if (current == null)
                            {
                                parent.rightChild = newnode;
                                return;
                            } // end of right hand
                        }
                    } // end of while

                }
            }
Exemplo n.º 16
0
        public Node LCA(Node root, Node n1, Node n2)
        {
            root = new Node(25);
            root.Left = new Node(20);
            root.Right = new Node(30);
            root.Left.Left = new Node(10);
            root.Left.Right = new Node(23);
            root.Right.Right = new Node(35);
            root.Left.Left.Left = new Node(5);
            root.Left.Right.Left = new Node(21);
            root.Right.Right.Left = new Node(32);

            while (root != null)
            {
            // If root>n1 and root>n2 then lowest common ancestor will be in
            // left
            // subtree.
            if (root.Value > n1.Value && root.Value > n2.Value)
            {
                root = root.Left;
            }
            // If root<n1 and root<n2 then lowest common ancestor will be in
            // right
            // subtree.
            else if (root.Value <= n1.Value && root.Value < n2.Value)
            {
                root = root.Right;
            }
            // if I am here that means i am at the root which is lowest common
            // ancestor
            else
            {
                return root;
            }
            }
            return root;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Populate the left child node and randomly populate the right child node of a node and
        /// their children nodes
        /// </summary>
        /// <param name="node">The node to be populate</param>
        /// <param name="level">The level of the tree</param>
        /// <returns>The node that was passed to, now with children</returns>
        private Node PopulateLeftRightNode(Node node, int level)
        {
            if (level < _maxDeepLevel)
            {
                level += 1;

                node.Left = PopulateLeftRightNode(new Node(node.Value + _baseX), level);

                //Randomly create the right child node
                if (_rnd.Next(1, 999) % 2 > 0)
                {
                    node.Right = PopulateLeftRightNode(new Node(node.Value + _baseY), level);
                }
            }

            return node;
        }
Exemplo n.º 18
0
        /// <summary>
        /// Create a binary tree
        /// </summary>
        private void CreateTree()
        {
            //Init the tree
            _tree = new BinaryTree();
            lblMsg.Text = @"Random binary tree created";

            //Add a new node, to the right side of the root node
            Node currentNode = new Node(0);
            _tree.Add(currentNode, false);

            //Populate its children nodes
            PopulateLeftRightNode(currentNode, 0);

            //Draw the tree
            PaintTree();

            //Update label
            lblCount.Text = _tree.Count.ToString();
        }
Exemplo n.º 19
0
 public BinaryTree()
 {
     RootNode = new Node(int.MinValue);
 }
Exemplo n.º 20
0
 /// adds a node to the tree
 public bool Add(Node node)
 {
     return RootNode.Add(node);
 }
Exemplo n.º 21
0
        //Time: O(n) & Space: O(1) (O(n) recursion overhead)
        public static void preorderRecur(Node node)
        {
            if (node == null)
                return;

            Console.Write(" {0} ", node.data);
            preorderRecur(node.left);
            preorderRecur(node.right);
        }
Exemplo n.º 22
0
 void Print(Node node)
 {
     if (node == null)
         return;
     Print(node.Left);
     Console.WriteLine(node.Val);
     Print(node.Right);
 }
Exemplo n.º 23
0
 public Node(int value, Node left, Node right)
 {
     Value = value;
     Left = left;
     Right = right;
 }
Exemplo n.º 24
0
 public Node(int data, Node left, Node right)
 {
     this.data = data;
     this.left = left;
     this.right = right;
 }
Exemplo n.º 25
0
        /// <summary>
        /// Process the node, insert it to the correct position in the dictionary
        /// </summary>
        /// <param name="node">The node to be processed</param>
        private void ProcessNode(Node node)
        {
            if (_nodeDictionary.ContainsKey(node.Value))
            {
                //Node already exists.
                //Add it to the list of node with the same value
                _nodeDictionary[node.Value].Add(node);
            }
            else
            {
                //New node
                //Create a list to store its value
                _nodeDictionary[node.Value] = new List<Node>();

                //Store the list as value of the key (node's value)
                _nodeDictionary[node.Value].Add(node);
            }
        }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            var cust = new Customer
            {
                Name = "Ferdo",
                LastName = "Mravec"
            };
            IEnumerable<Customer> list = new List<Customer>
            {
                cust, 
                cust,
                cust
            };

            UserRights rights = UserRights.Login | UserRights.Edit;
            var ss = rights.HasFlag(UserRights.Edit);
            
            foreach (var right in EnumHelper.GetValues<UserRights>())
            {
                Console.WriteLine(right);
            }

            list.Vypis<Customer>();

            var list2 = new List<int> {1, 2, 3, 4};
            list2.Vypis<int>();

            // Binary tree
            var strom = new Node<int>(2);
            strom.Insert(8);
            strom.Insert(16);
            strom.Insert(4); 
            strom.Insert(9);
            strom.Insert(1);

            var sw = new Stopwatch();
            sw.Start();
            var rnd = new Random();
            for (int i = 0; i <= 20000; i++)
            {
                strom.Insert(rnd.Next(200000000));
            }
            sw.Stop();
            Console.WriteLine("Elapsed time: {0}", sw.Elapsed.Seconds);

            Console.WriteLine(strom.HasValue(3)); //false
            Console.WriteLine(strom.HasValue(9)); //true

            //strom.Vypis();

           // ArrayList nonGenericList = new ArrayList();
            List<int> genericList = new List<int>();

            var telefonnyZoznam = new Dictionary<int, string>
            {
                {148198349, "Ferdo Mravec"},
                {448148349, "CFerdo Mravec"},
                {348148349, "BFerdo Mravec"},
                {241498349, "AFerdo Mravec"}
            };

            Console.WriteLine("Dictionary");
            foreach (var keyValue in telefonnyZoznam)
            {
                Console.WriteLine("{0} {1}", keyValue.Key, keyValue.Value);
            }

            Console.WriteLine("Sorted List");
            var telefonnyZoznam2 = new SortedList<int, string>
            {
                {148198349, "Ferdo Mravec"},
                {448148349, "CFerdo Mravec"},
                {348148349, "BFerdo Mravec"},
                {241498349, "AFerdo Mravec"}
            };

            foreach (var keyValue in telefonnyZoznam2)
            {
                Console.WriteLine("{0} {1}", keyValue.Key, keyValue.Value);
            }
            
            //  Hashtable hashtable = new Hashtable();
            Dictionary<int, string> genericDictionary  = new Dictionary<int, string>();

            SortedList<int, string> sortedList = new SortedList<int, string>();
            HashSet<int> hashSet = new HashSet<int>();
       
            Stack<int> stack = new Stack<int>();
            stack.Push(2);
            stack.Push(3); 
            stack.Push(4);
            var stackTop = stack.Peek();
            stackTop = stack.Pop();

            Queue<int> queue = new Queue<int>();
            queue.Enqueue(2);
            queue.Dequeue();

            ConcurrentDictionary<int, string> dictionary;
            //http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx


            MyCustomDelegate pointerToFunction = NamedFunction;
            pointerToFunction("halo", 1);

            pointerToFunction = delegate(string input, int value)
            {
                var textMessage = value + input;
                Console.WriteLine(textMessage);
            };

            //Lamdba function
            pointerToFunction = (input, value) =>
            {
                var textMessage = value + input;
                Console.WriteLine(textMessage);
            };

            MyCustomIntDelegate pointerToIntFunction =
                (input, value) =>
                {
                    return value * 2;
                };

            //ExpressionLambda
            pointerToIntFunction = 
                (input, value) => value * 2;

            Func<int> funcInt = () => 2*2;
            Func<string, int> funcStringInt = s => s.Length;
            GetLength funcGetLength = s => s.Length;

            Action funcDummy = () => Console.WriteLine("hello");
            Action<int> actionInt = i => Console.WriteLine("hello" + i);

        }
Exemplo n.º 27
0
 void InsertHelper(Node curr, Node node)
 {
     if (node.Val < curr.Val)
     {
         if (curr.Left == null)
             curr.Left = node;
         else
             InsertHelper(curr.Left, node);
     }
     else
     {
         if (curr.Right == null)
             curr.Right = node;
         else
             InsertHelper(curr.Right, node);
     }
 }
Exemplo n.º 28
0
 public static bool IsValidBST(Node root)
 {
     return Check(root, new Stack<Node>());
 }
Exemplo n.º 29
0
        /// <summary>
        /// Traverse a binary tree in InOrder order using Morris' algorithm
        /// http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ 
        /// http://geeksquiz.com/threaded-binary-tree/
        /// </summary>
        /// <param name="root">The root node of a binary tree</param>
        private void TraveseInOrderUsingMorris(Node root)
        {
            Node current = root;
            Node predecessor = null;

            //Reset the dict
            _nodeDictionary = new Dictionary<int, List<Node>>();

            while (current != null)
            {
                if (current.Left == null) //The leftmost node of current branch
                {
                    //Process the node
                    ProcessNode(current);
                    current = current.Right;
                }
                else
                {
                    //Find the inorder predecessor of current
                    predecessor = current.Left;

                    while (predecessor.Right != null && predecessor.Right != current)
                    {
                        predecessor = predecessor.Right; //The rightmost node of current branch
                    }

                    //Make current as right child of its inorder predecessor
                    if (predecessor.Right == null)
                    {
                        predecessor.Right = current; //Make a thread link to current
                        current = current.Left;
                    }
                    else
                    {
                        //Revert the changes made to restore the original tree
                        //i.e., remove the thread link to current (the right child of predecssor)
                        predecessor.Right = null;

                        //Process the node
                        ProcessNode(current);
                        current = current.Right;
                    }
                }
            }
        }
Exemplo n.º 30
0
 /// <summary>
 /// Delete a node from the binary tree
 /// </summary>
 /// <param name="node">Node reference</param>
 public void Delete(Node node)
 {
     Delete(node.Val);
 }