Exemple #1
0
        protected IBinaryNode <K, V> GetParent(IBinaryNode <K, V> Subroot, K Key)
        {
            if (Subroot == null)
            {
                return(null);                // Not found;
            }
            int compare = Key.CompareTo(Subroot.Key);

            if (compare == 0)
            {
                return(Subroot);
            }

            if (Subroot.Left != null && Key.Equals(Subroot.Left.Key))
            {
                return(Subroot);
            }

            else if (Subroot.Right != null && Key.Equals(Subroot.Right.Key))
            {
                return(Subroot);
            }

            if (compare < 0)
            {
                return(GetParent(Subroot.Left, Key));
            }
            return(GetParent(Subroot.Right, Key));
        }
    public IEnumerable <IBinaryNode <T> > Traverse(IBinaryNode <T> current, IEnumerable <IBinaryNode <T> > recursionData)
    {
        // no children found
        if (RightChild == null && LeftChild == null)
        {
            //correct guess BinaryNode has the needed data
            if (current.Data.Equals(Data))
            {
                return(recursionData);
            }

            //wrong value - try another leg
            return(null);
        }

        //there are children
        IEnumerable <IBinaryNode <T> > left  = null; //tmp left storage
        IEnumerable <IBinaryNode <T> > right = null; //tmp right storage

        //start with the left child
        //and travers in depth by left leg
        if (LeftChild != null)
        {
            //go in depth through the left child
            var leftPath = new List <IBinaryNode <T> >();

            //add previously gathered recursionData
            leftPath.AddRange(recursionData);

            leftPath.Add(LeftChild);

            //recursion call by rigth leg
            left = LeftChild.Traverse(current, leftPath);
        }

        //no left children found
        //travers by right leg in depth now
        if (RightChild != null)
        {
            //go in depth through the right child
            var rightPath = new List <IBinaryNode <T> >();

            //add previously gathered recursionData
            rightPath.AddRange(recursionData);

            //add current value
            rightPath.Add(RightChild);

            //recursion call by rigth leg
            right = RightChild.Traverse(current, rightPath);
        }

        //return collected value of left or right
        if (left != null)
        {
            return(left);
        }

        return(right);
    }
 public DecomposeTreesAndComposeItAgain()
 {
     root                    = new BinaryNode <int>(7);
     root.LeftNode           = new BinaryNode <int>(5);
     root.RightNode          = new BinaryNode <int>(15);
     root.RightNode.LeftNode = new BinaryNode <int>(11);
 }
 public FindingMinimalValueInBinaryTree()
 {
     root                    = new BinaryNode <int>(7);
     root.LeftNode           = new BinaryNode <int>(5);
     root.RightNode          = new BinaryNode <int>(15);
     root.RightNode.LeftNode = new BinaryNode <int>(11);
 }
Exemple #5
0
    public void Build(IEnumerable <T> source)
    {
        foreach (var item in source)
        {
            var node = new BinaryNode <T>(item, nodeId);
            nodeId++;
            nodes.Add(node);
        }

        //construct a tree
        while (nodes.Count > 1)     //while more than one node in a list
        {
            var taken = nodes.Take(2).ToList();

            // Create a parent BinaryNode and sum probabilities
            var parent = new BinaryNode <T>()
            {
                LeftChild  = taken[0],
                RightChild = taken[1]
            };

            //this has been added so remove from the topmost list
            nodes.Remove(taken[0]);
            nodes.Remove(taken[1]);
            nodes.Add(parent);
        }

        Root = nodes.FirstOrDefault();
    }
        private void ComposeTreeAgain(IBinaryNode <int> root, IDictionary <int, NodeData <int>[]> nodeDataCollection, int indexNode)
        {
            if (!nodeDataCollection.ContainsKey(indexNode))
            {
                return;
            }

            var nodes = nodeDataCollection[indexNode];

            if (indexNode == -1)
            {
                root.Value = nodes[0].Value;
                indexNode++;
                ComposeTreeAgain(root, nodeDataCollection, indexNode);
            }
            else
            {
                if (nodes[0].Index != 0)
                {
                    root.LeftNode = new BinaryNode <int>(nodes[0].Value);
                    ComposeTreeAgain(root.LeftNode, nodeDataCollection, nodes[0].Index);
                }
                if (nodes[1].Index != 0)
                {
                    root.RightNode = new BinaryNode <int>(nodes[1].Value);
                    ComposeTreeAgain(root.RightNode, nodeDataCollection, nodes[1].Index);
                }
            }
        }
        private void DecomposeTree(IBinaryNode <int> root, IDictionary <int, NodeData <int>[]> nodeDataCollection, ref int indexNode)
        {
            if (root == null)
            {
                return;
            }
            int parentIndex = indexNode;

            if (!nodeDataCollection.ContainsKey(indexNode))
            {
                nodeDataCollection[indexNode] = new NodeData <int> [2];
            }
            if (root.LeftNode != null)
            {
                indexNode++;
                nodeDataCollection[parentIndex][0] = new NodeData <int>(indexNode, root.LeftNode.Value);
                DecomposeTree(root.LeftNode, nodeDataCollection, ref indexNode);
            }

            if (root.RightNode != null)
            {
                indexNode++;
                nodeDataCollection[parentIndex][1] = new NodeData <int>(indexNode, root.RightNode.Value);
                DecomposeTree(root.RightNode, nodeDataCollection, ref indexNode);
            }
        }
Exemple #8
0
 public BinaryNode(T value, IBinaryNode <T> left, IBinaryNode <T> right) : this(value)
 {
     Left        = left;
     Right       = right;
     Parent      = null;
     Left.Parent = Right.Parent = this;
 }
Exemple #9
0
        public bool MoveNext()
        {
            if (!isYieldedStart)
            {
                isYieldedStart = true;
                return(true);
            }

            if (CurrentNode.Right != null)
            {
                CurrentNode = CurrentNode.Right;
                while (CurrentNode.Left != null)
                {
                    CurrentNode = CurrentNode.Left;
                }

                return(true);
            }
            else
            {
                var p = CurrentNode.Parent;
                while (p != null && CurrentNode == p.Right)
                {
                    CurrentNode = p;
                    p           = p.Parent;
                }

                CurrentNode = p;

                return(CurrentNode != null);
            }
        }
 protected T MinValue(IBinaryNode <T> root)
 {
     if (root.GetLeftNode() != null)
     {
         return(MinValue(root.GetLeftNode()));
     }
     return(root.GetData());
 }
 protected void PrintInOrder(IBinaryNode <T> root)
 {
     if (root != null)
     {
         PrintInOrder(root.GetLeftNode());
         Console.WriteLine(root.GetData().ToString() + " \n");
         PrintInOrder(root.GetRightNode());
     }
 }
Exemple #12
0
 public InorderIterator(IBinaryNode <T> rootNode)
 {
     RootNode    = rootNode;
     CurrentNode = RootNode;
     while (CurrentNode.Left != null)
     {
         CurrentNode = CurrentNode.Left;
     }
 }
 private int MinmalValue(IBinaryNode <int> root, int min)
 {
     if (root == null)
     {
         return(min);
     }
     min = Math.Min(min, root.Value);
     return(Math.Min(MinmalValue(root.LeftNode, min), MinmalValue(root.RightNode, min)));
 }
Exemple #14
0
 public void InOrder(IBinaryNode <K, V> Subroot, ITreeVisitor <K, V> v)
 {
     if (Subroot == null)
     {
         return;
     }
     InOrder(Subroot.Left, v);
     v.Visit(Subroot);
     InOrder(Subroot.Right, v);
 }
Exemple #15
0
 private void WidthTraversal(IBinaryNode <T> current)
 {
     foreach (var node in current)
     {
         if (OnTraverse != null)
         {
             OnTraverse((IBinaryNode <T>)node);
         }
     }
 }
Exemple #16
0
        public virtual bool Add(K Key, V Value)
        {
            if (Root == null)
            {
                Root = new BinaryNode <K, V>(Key, Value);
                return(true);
            }

            return(Add(Root, Key, Value));
        }
Exemple #17
0
        protected IBinaryNode <T> DeleteNode(IBinaryNode <T> node, T data)
        {
            try
            {
                if (node == null)
                {
                    return(node);
                }

                if (Comparer.Compare(data, node.GetData()) < 0)
                {
                    node.SetLeftNode(DeleteNode(node.GetLeftNode(), data));
                }
                else if (Comparer.Compare(data, node.GetData()) > 0)
                {
                    node.SetRightNode(DeleteNode(node.GetRightNode(), data));
                }
                else
                {
                    // node with no leaf nodes
                    if (node.GetLeftNode() == null && node.GetRightNode() == null)
                    {
                        return(null);
                    }
                    else if (node.GetLeftNode() == null)
                    {
                        // node with one node (no left node)

                        return(node.GetRightNode());
                    }
                    else if (node.GetRightNode() == null)
                    {
                        // node with one node (no right node)

                        return(node.GetLeftNode());
                    }
                    else
                    {
                        // nodes with two nodes
                        // search for min number in right sub tree
                        T minValues = MinValue(node.GetRightNode());
                        node.SetData(minValues);
                        node.SetRightNode(DeleteNode(node.GetRightNode(), minValues));
                    }
                }

                return(node);
            }

            catch (NullReferenceException ex)
            {
                throw ex;
            }
        }
Exemple #18
0
        private void Print(IBinaryNode <T> rootNode)
        {
            if (rootNode == null)
            {
                return;
            }

            Print(rootNode.Left);
            Console.WriteLine(rootNode.Value);
            Print(rootNode.Right);
        }
 private void TraversePreorder(IBinaryNode <int> node)
 {
     Console.WriteLine(node.Data);
     if (node.LeftNode != null)
     {
         TraversePreorder(node.LeftNode);
     }
     if (node.RightNode != null)
     {
         TraversePreorder(node.RightNode);
     }
 }
 protected bool RContains(IBinaryNode <T> root, T data)
 {
     if (root == null)
     {
         return(false);
     }
     if (root.GetData().Equals(data))
     {
         return(true);
     }
     return(RContains(root.GetLeftNode(), data) || RContains(root.GetRightNode(), data));
 }
        protected static IBinaryNode <T> DeleteTree(IBinaryNode <T> root)
        {
            if (root != null)
            {
                root.SetLeftNode(DeleteTree(root.GetLeftNode()));
                root.SetRightNode(DeleteTree(root.GetLeftNode()));
                root = null;

                return(null);
            }
            return(null);
        }
 public DecomposeTreeIntoStringAndComposeItAgain()
 {
     root                             = new BinaryNode <int>(7);
     root.LeftNode                    = new BinaryNode <int>(5);
     root.LeftNode.LeftNode           = new BinaryNode <int>(3);
     root.LeftNode.RightNode          = new BinaryNode <int>(4);
     root.LeftNode.RightNode.LeftNode = new BinaryNode <int>(18);
     root.RightNode                   = new BinaryNode <int>(15);
     root.RightNode.LeftNode          = new BinaryNode <int>(10);
     root.RightNode.RightNode         = new BinaryNode <int>(11);
     root.RightNode.LeftNode.LeftNode = new BinaryNode <int>(14);
 }
Exemple #23
0
        void PrintByInorder(IBinaryNode <T> node)
        {
            if (node == null)
            {
                return;
            }
            var left  = node.Left;
            var right = node.Right;

            PrintByPostorder(left);
            System.Console.Write(" " + node.ToString());
            PrintByPostorder(right);
        }
Exemple #24
0
 public void TraversePostorder(IBinaryNode <int> node)
 {
     if (node.LeftNode != null)
     {
         TraversePostorder(node.LeftNode);
     }
     //Console.WriteLine(node.Data);
     if (node.RightNode != null)
     {
         TraversePostorder(node.RightNode);
     }
     Console.WriteLine(node.Data);
 }
 private void MinmalValue(IBinaryNode <int> root, ref int currentMin)
 {
     if (root == null)
     {
         return;
     }
     if (root.Value < currentMin)
     {
         currentMin = root.Value;
     }
     MinmalValue(root.LeftNode, ref currentMin);
     MinmalValue(root.RightNode, ref currentMin);
 }
Exemple #26
0
 private bool check(IBinaryNode <int> roo, int min, int max)
 {
     if (roo == null)
     {
         return(true);
     }
     if (roo.Data > min && roo.Data < max &&
         check(roo.LeftNode, min, roo.Data) &&
         check(roo.RightNode, root.Data, max))
     {
         return(true);
     }
     return(false);
 }
        protected IBinaryNode <T> FindNode(IBinaryNode <T> node, T data)
        {
            if (node == null)
            {
                return(node);
            }
            if (node.GetData().Equals(data))
            {
                return(node);
            }

            if (Comparer.Compare(data, node.GetData()) < 0)
            {
                return(FindNode(node.GetLeftNode(), data));
            }
            else if (Comparer.Compare(data, node.GetData()) > 0)
            {
                return(FindNode(node.GetRightNode(), data));
            }
            else
            {
                // node with no leaf nodes
                if (node.GetLeftNode() == null && node.GetRightNode() == null)
                {
                    return(null);
                }
                else if (node.GetLeftNode() == null)
                {
                    // node with one node (no left node)

                    return(node.GetRightNode());
                }
                else if (node.GetRightNode() == null)
                {
                    // node with one node (no right node)

                    return(node.GetLeftNode());
                }
                else
                {
                    // nodes with two nodes
                    // search for min number in right sub tree
                    T minValues = MinValue(node.GetRightNode());
                    node.SetData(minValues);
                    node.SetRightNode(FindNode(node.GetRightNode(), minValues));
                }
                return(node);
            }
        }
        public static Leaf GetNextLeaf(this IBinaryNode current)
        {
            var next = current.GetNext();

            while (next != null)
            {
                if (next is Leaf leaf)
                {
                    return(leaf);
                }

                next = next.GetNext();
            }

            return(null);
        }
        public static Leaf GetPreviousLeaf(this IBinaryNode current)
        {
            var prev = current.GetPrevious();

            while (prev != null)
            {
                if (prev is Leaf leaf)
                {
                    return(leaf);
                }

                prev = prev.GetPrevious();
            }

            return(null);
        }
        private void SetNodeInfo_DG(IBaseNode pBaseNode, View.NodeViewItem treeNode)
        {
            if (pBaseNode == null)
            {
                return;
            }
            //
            switch (pBaseNode.eNodeStyle)
            {
            case NodeStyle.eBinaryNode:
                View.NodeViewItem treeNode1 = new View.NodeViewItem(Language.LanguageStrategy.SquareBrackets_Left + Language.LanguageStrategy.BinaryNodeText + Language.LanguageStrategy.SquareBrackets_Right + pBaseNode.Text + Language.LanguageStrategy.RoundBrackets_Left + pBaseNode.Name + Language.LanguageStrategy.RoundBrackets_Right);
                treeNode.NodeViewItems.Add(treeNode1);
                IBinaryNode pBinaryNode = pBaseNode as IBinaryNode;
                if (pBinaryNode != null)
                {
                    this.SetNodeInfo_DG(pBinaryNode.LeftNode, treeNode1);
                    this.SetNodeInfo_DG(pBinaryNode.RightNode, treeNode1);
                }
                break;

            case NodeStyle.eMultipleNode:
                View.NodeViewItem treeNode2 = new View.NodeViewItem(Language.LanguageStrategy.SquareBrackets_Left + Language.LanguageStrategy.MultipleNodeText + Language.LanguageStrategy.SquareBrackets_Right + pBaseNode.Text + Language.LanguageStrategy.RoundBrackets_Left + pBaseNode.Name + Language.LanguageStrategy.RoundBrackets_Right);
                treeNode.NodeViewItems.Add(treeNode2);
                IMultipleNode pMultipleNode = pBaseNode as IMultipleNode;
                if (pMultipleNode != null)
                {
                    foreach (IBaseNode one in pMultipleNode.ChildNodes)
                    {
                        this.SetNodeInfo_DG(one, treeNode2);
                    }
                }
                break;

            case NodeStyle.eBottomNode:
                View.ImageNodeViewItem treeNode3 = new View.ImageNodeViewItem(Language.LanguageStrategy.SquareBrackets_Left + Language.LanguageStrategy.BottomNodeText + Language.LanguageStrategy.SquareBrackets_Right + pBaseNode.Text + Language.LanguageStrategy.RoundBrackets_Left + pBaseNode.Name + Language.LanguageStrategy.RoundBrackets_Right);
                BasePanel basePanel = pBaseNode as BasePanel;
                if (basePanel != null)
                {
                    treeNode3.Image = basePanel.Image;
                }
                treeNode.NodeViewItems.Add(treeNode3);
                break;

            default:
                break;
            }
        }