Example #1
0
 public object Pop()
 {
     if (count == 0)
         throw new Exception("Stack is empty");
     Node current = top;
     top = top.Next;
     count--;
     return current.Data;
 }
Example #2
0
 public void Push(object item)
 {
     Node newNode = new Node(item);
     if (top == null)
     {
         top = newNode;
     }
     else
     {
         Node temp = top;
         top = newNode;
         top.Next = temp;
     }
     count++;
 }
Example #3
0
        static ulong FindMaxPath(Node root)
        {
            foreach (var child in root.Children)
            {
                root.PathLengths.Add(FindMaxPath(child));
            }

            if (root.PathLengths.Count != 0)
            {
                return root.Value + root.PathLengths.Max();
            }
            else
            {
                return root.Value;
            }
        }
Example #4
0
        public CbinaryTree(byte[] in_data)
        {
            try
            {
                sum_nodes = CountNodes(in_data);
                nodes = new Node[sum_nodes];
                depth_reaf_num = (byte[])in_data.Clone();
                for (int i = 0; i < sum_nodes; i++)
                {
                    nodes[i] = new Node();
                }

                MakeDepthTable(in_data);
                CountReaves(in_data);
                SetNodes(in_data);
                SetTReaf();
            }
            catch
            {
                Console.WriteLine("");
            }
        }
 public Node Pop()
 {
     if (Head == null)
     return null;
       Node poppedNode = Head;
       Head = Head.Next;
       return poppedNode;
 }
 public void Push(Node node)
 {
     node.Next = Head;
       Head = node;
 }
Example #7
0
 public void AddChild(Node child)
 {
     this.Children.Add(child);
     this.HasChildren = true;
     child.HasParent = true;
 }
Example #8
0
 public void Clear()
 {
     this.top = null;
     this.count = 0;
 }
Example #9
0
 public Stack()
 {
     this.top = null;
     this.count = 0;
 }
Example #10
0
 public Node(object data)
 {
     this.data = data;
     this.next = null;
 }
Example #11
0
        public void delete(T val)
        {
            Node<T> node = findNode(root, val);
            if (node == null)
                return;
            else if (node.isLeaf())
            {
                if (node == root)
                    root = null;
                else if (node.Parent.Right == node)
                    node.Parent.Right = null;
                else
                    node.Parent.Left = null;
            }
            else if (node.hasOneChild())
            {
                if (node == root)
                {
                    if (node.Left != null)
                    {
                        root = node.Left;
                    }
                    else
                    {
                        root = node.Right;
                    }
                    root.Parent = null;
                }
                else if (node.Parent.Left == node)
                {
                    if (node.Left != null)
                    {
                        node.Parent.Left = node.Left;
                        node.Left.Parent = node.Parent;
                    }
                    else
                    {
                        node.Parent.Left = node.Right;
                        node.Right.Parent = node.Parent;
                    }
                }
                else
                {
                    if (node.Left != null)
                    {
                        node.Parent.Right = node.Left;
                        node.Left.Parent = node.Parent;
                    }
                    else
                    {
                        node.Parent.Right = node.Right;
                        node.Right.Parent = node.Parent;
                    }
                }
            }
            else if (node.hasTwoChild())
            {
                Node<T> successor = node.Right;
                while (successor.Left != null)
                    successor = successor.Left;

                delete(successor.val);

                successor.Right = node.Right;
                successor.Left = node.Left;
                if (node.Left != null)
                    node.Left.Parent = successor;
                if (node.Right != null)
                    node.Right.Parent = successor;
                successor.Parent = node.Parent;
                if (node == root)
                    root = successor;
                else if (node.Parent.Right == node)
                    node.Parent.Right = successor;
                else if(node.Parent.Left==node)
                    node.Parent.Left = successor;
            }
        }