Ejemplo n.º 1
0
        public CNode ParentFinder(CNode n1, CNode n2)
        {
            PrintPreliminatyParentFindingInfo(n1, n2);

            CNode n1Parent = n1.ParentNodesStack.Pop();
            CNode n2Parent = n2.ParentNodesStack.Pop();

            while (n1Parent.Name != n2Parent.Name &&
                   n1Parent != null &&
                   n2Parent != null)
            {
                if (n1Parent.Depth > n2Parent.Depth)
                {
                    n1Parent = n1.ParentNodesStack.Pop();
                }
                else if (n1Parent.Depth < n2Parent.Depth)
                {
                    n2Parent = n2.ParentNodesStack.Pop();
                }
                else
                {
                    n1Parent = n1.ParentNodesStack.Pop();
                    n2Parent = n2.ParentNodesStack.Pop();
                }
            }

            if (n1Parent.Name == n2Parent.Name)
            {
                return(n1Parent);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
 private void SetDataForChildNode(CNode node, CNode Child)
 {
     Child.Depth            = node.Depth + 1;
     Child.ParentNodesStack = new Stack <CNode>(new Stack <CNode>(node.ParentNodesStack));
     Child.ParentNodesStack.Push(node);
     Child.Parent = node;
 }
Ejemplo n.º 3
0
        public void Map(CNode node, CBTree tree)
        {
            if (node.IsLeaf)
            {
                return;
            }

            CNode RightChild = node.RightChild;
            CNode LeftChild  = node.LeftChild;

            if (RightChild != null)
            {
                SetDataForChildNode(node, RightChild);

                if (RightChild.Depth > tree.MaxDepth)
                {
                    tree.MaxDepth = RightChild.Depth;
                }

                Map(RightChild, tree);
            }

            if (LeftChild != null)
            {
                SetDataForChildNode(node, LeftChild);

                if (LeftChild.Depth > tree.MaxDepth)
                {
                    tree.MaxDepth = LeftChild.Depth;
                }

                Map(LeftChild, tree);
            }
        }
Ejemplo n.º 4
0
        private void DrawBranch(double maxDepth, int currentDepth, CNode node, CNode commonParent)
        {
            if (node == null)
            {
                return;
            }

            if (currentDepth == 0)
            {
                Console.Write(GetNodeHeader() + node.Name);
            }
            else if (currentDepth == 1)
            {
                Console.Write(GetDepthSeperatorForDepth1() + GetNodeHeader() + node.Name);
            }
            else if (currentDepth >= 2)
            {
                Console.Write(GetDepthSeperatorForDepth1());

                for (int i = 2; i <= currentDepth; i++)
                {
                    Console.Write(GetDepthSeperatorGreaterThan1());
                }

                Console.Write(GetNodeHeader() + node.Name);
            }

            if (node.Name == commonParent.Name)
            {
                Console.WriteLine(GetCommonParentString());
            }
            else
            {
                Console.WriteLine();
            }

            if (!node.IsLeaf)
            {
                DrawBranch(maxDepth, currentDepth + 1, node.LeftChild, commonParent);
                DrawBranch(maxDepth, currentDepth + 1, node.RightChild, commonParent);
            }
        }
Ejemplo n.º 5
0
        static void Main()
        {
            CBTreeAndNodeTools TreeTools = new CBTreeAndNodeTools();

            Console.WriteLine("Example 1");

            // Creating Nodes for the tree
            CNode leaf1 = new CNode
            {
                Name             = "leaf1",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode leaf2 = new CNode
            {
                Name             = "leaf2",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode leaf3 = new CNode
            {
                Name             = "leaf3",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode leaf4 = new CNode
            {
                Name             = "leaf4",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode node2 = new CNode
            {
                Name             = "node2",
                IsLeaf           = false,
                RightChild       = leaf1,
                LeftChild        = leaf2,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode node1 = new CNode
            {
                Name             = "node1",
                IsLeaf           = false,
                RightChild       = node2,
                LeftChild        = leaf3,
                ParentNodesStack = new Stack <CNode>()
            };


            // Create the root node
            // This is the only input necessary to build the tree which is then mapped based on the root node
            CNode head = new CNode
            {
                Name             = "head",
                IsLeaf           = false,
                RightChild       = node1,
                LeftChild        = leaf4,
                Depth            = 0,
                ParentNodesStack = new Stack <CNode>()
            };

            CBTree tree = new CBTree {
                RootNode = head
            };


            // tree mapping
            TreeTools.Map(tree);


            // Alter this function to search for a different Common parent between different nodes
            CNode CommonParent = TreeTools.ParentFinder(leaf1, leaf3);

            TreeTools.Draw(tree, CommonParent);

            //-------------------EXAMPLE 2------------------//

            Console.WriteLine();
            Console.WriteLine("Example 2");


            // Creating Nodes for the tree
            leaf1 = new CNode
            {
                Name             = "leaf1",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            leaf2 = new CNode
            {
                Name             = "leaf2",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            leaf3 = new CNode
            {
                Name             = "leaf3",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            leaf4 = new CNode
            {
                Name             = "leaf4",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode leaf5 = new CNode
            {
                Name             = "leaf5",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode leaf6 = new CNode
            {
                Name             = "leaf6",
                IsLeaf           = true,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode node4 = new CNode
            {
                Name             = "node4",
                IsLeaf           = false,
                RightChild       = leaf3,
                LeftChild        = leaf5,
                ParentNodesStack = new Stack <CNode>()
            };

            CNode node3 = new CNode
            {
                Name             = "node3",
                IsLeaf           = false,
                RightChild       = leaf6,
                LeftChild        = node4,
                ParentNodesStack = new Stack <CNode>()
            };

            node2 = new CNode
            {
                Name             = "node2",
                IsLeaf           = false,
                RightChild       = leaf1,
                LeftChild        = leaf2,
                ParentNodesStack = new Stack <CNode>()
            };

            node1 = new CNode
            {
                Name             = "node1",
                IsLeaf           = false,
                RightChild       = node2,
                LeftChild        = node3,
                ParentNodesStack = new Stack <CNode>()
            };


            // Create the root node
            // This is the only input necessary to build the tree which is then mapped based on the root node
            head = new CNode
            {
                Name             = "head",
                IsLeaf           = false,
                RightChild       = node1,
                LeftChild        = leaf4,
                Depth            = 0,
                ParentNodesStack = new Stack <CNode>()
            };

            tree = new CBTree {
                RootNode = head
            };


            // tree mapping
            TreeTools.Map(tree);


            // Alter this function to search for a different Common parent between different nodes
            CommonParent = TreeTools.ParentFinder(leaf6, leaf5);

            TreeTools.Draw(tree, CommonParent);

            TreeTools.PrintExitLine();

            Console.ReadKey();
        }
Ejemplo n.º 6
0
 private static void PrintFailedParentFindingInfo(CNode n1, CNode n2)
 {
     Console.WriteLine(GetFailedCommonParentString() + n1.Name + And() + n2.Name + Period());
     Console.WriteLine("");
 }
Ejemplo n.º 7
0
 public void Draw(CBTree tree, CNode commonParent)
 {
     DrawBranch(tree.MaxDepth, 0, tree.RootNode, commonParent);
 }