Exemple #1
0
        public Node search(NodeTree tree, String targetString)
        {
            // memory to maintain unvisited children

            Stack queue = new Stack();

            // dfs

            // read first child each node

            // if has first child

            //      store node

            //      current node = child node

            //  else

            //      print node data

            //      pop next node off stack

            queue.push(tree.GetTreeA());

            Node currentNode = null;

            while ((currentNode = queue.pop()) != null)
            {
                Node nextNode = null;

                while ((nextNode = currentNode.getNextChild()) != null)
                {
                    if (nextNode != null)
                    {
                        queue.push(currentNode);

                        currentNode = nextNode;
                    }
                    else
                    {
                        Console.WriteLine("Node Data: " + currentNode.getNodeStr());
                    }
                }

                Console.WriteLine("Node Data: " + currentNode.getNodeStr());
            }

            return(null);
        }
Exemple #2
0
        public DisplayTree search(NodeTree tree, String targetString)
        {
            // memory to maintain unvisited children

            List <Node> queue = new List <Node>();

            // iterate over all nodes (breadth first search)

            queue.Add(tree.GetTreeA());

            int currentLevelCount = 1;

            int nextLevelCount = 0;

            int currentLevel = 1;

            Console.WriteLine("First Level: " + currentLevel);

            Node currentNode = null;

            // counter to begin iterating over newly added children to instantiate display nodes

            int displayIterBegin = 1;

            // counter to end iterating over newly added children to instantiate display nodes

            int displayIterEnd = 1;

            // test if display tree has been instantiated

            if (displayTree == null)
            {
                displayTree = new DisplayTree(width, height, nodeSize);
            }

            DisplayNode currentParentDisplayNode = new DisplayNode();

            // add the root display node

            displayTree.AddDisplayNode(queue.ElementAt(0), null, currentLevel, 1, nextLevelCount, currentParentDisplayNode);

            List <DisplayNode> childDisplayNodes = new List <DisplayNode>();

            List <DisplayNode> parentDisplayNodes = new List <DisplayNode>();

            parentDisplayNodes.Add(currentParentDisplayNode);

            List <Node> childNodes = new List <Node>();

            while (queue.Count > 0)
            {
                currentNode = queue.ElementAt(0);

                queue.RemoveAt(0);

                // get current parent display node

                currentParentDisplayNode = parentDisplayNodes.ElementAt(0);

                parentDisplayNodes.RemoveAt(0);

                Console.WriteLine("Current Node Str: " + currentNode.getNodeStr());

                Node currentNodeChild = null;

                while ((currentNodeChild = currentNode.getNextChild()) != null)
                {
                    queue.Add(currentNodeChild);

                    nextLevelCount++;

                    displayIterEnd++;

                    DisplayNode displayNode = new DisplayNode();

                    displayNode.SetParent(currentParentDisplayNode);

                    childDisplayNodes.Add(displayNode);

                    childNodes.Add(currentNodeChild);
                }

                currentLevelCount--;

                if (queue.Count > 0)
                {
                    if (currentLevelCount == 0)
                    {
                        parentDisplayNodes.Clear();

                        currentLevel++;

                        for (int levelCounter = 0; levelCounter < childDisplayNodes.Count; levelCounter++)
                        {
                            // add the display node

                            displayTree.AddDisplayNode(childNodes.ElementAt(levelCounter), childDisplayNodes.ElementAt(levelCounter).GetParent(), currentLevel, nextLevelCount, levelCounter, childDisplayNodes.ElementAt(levelCounter));

                            parentDisplayNodes.Add(childDisplayNodes.ElementAt(levelCounter));
                        }

                        Console.WriteLine("Level has incremented! " + currentLevel);

                        currentLevelCount = nextLevelCount;

                        nextLevelCount = 0;

                        childDisplayNodes.Clear();

                        childNodes.Clear();
                    }
                }
            }

            Console.WriteLine("Final Current Level: " + currentLevel);

            displayTree.CalculateDisplayTreeYValues(currentLevel);

            return(displayTree);
        }