Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Problem I:");
            List<string> problemI = new List<string>() { "MARCH", "MAY", "NOVEMBER", "AUGUST",
                "APRIL", "JANUARY", "DECEMBER", "JULY", "FEBRUARY", "JUNE", "OCTOBER", "SEPTEMBER" };
            Node[] problemINodes = new Node[problemI.Count];
            AssignNodes(problemI, problemINodes);
            Tree problemITree = new Tree(problemINodes[0]);
            BuildTree(problemITree, problemINodes);
            problemITree.InOrderTraversal(problemITree.rootNode);
            Console.WriteLine("--------------");
            ClearTreeByDeletion(problemITree, problemINodes);

            Console.ReadLine();
            Console.WriteLine();

            Console.WriteLine("Problem II:");
            List<string> problemII = new List<string>() { "DECEMBER", "JANUARY", "APRIL", "MARCH",
                "JULY", "AUGUST", "OCTOBER", "FEBRUARY", "NOVEMBER", "MAY", "JUNE", "SEPTEMBER" };
            Node[] problemIINodes = new Node[problemII.Count];
            AssignNodes(problemII, problemIINodes);
            Tree problemIITree = new Tree(problemIINodes[0]);
            BuildTree(problemIITree, problemIINodes);
            problemIITree.InOrderTraversal(problemIITree.rootNode);
            Console.WriteLine("--------------");
            ClearTreeByDeletion(problemIITree, problemIINodes);

            Console.ReadLine();
            Console.WriteLine();

            int size = 10;
            bool lowerCase = false;
            List<string> problemIII = new List<string>();
            BuildRandomStringList(size, lowerCase, problemIII);
            Node[] problemIIINodes = new Node[problemIII.Count];
            AssignNodes(problemIII, problemIIINodes);
            Tree problemIIITree = new Tree(problemIIINodes[0]);
            BuildTree(problemIIITree, problemIIINodes);
            problemIIITree.InOrderTraversal(problemIIITree.rootNode);
            Console.WriteLine("--------------");
            ClearTreeByDeletion(problemIIITree, problemIIINodes);
            Console.ReadLine();
        }
Exemplo n.º 2
0
 private static void BuildTree(Tree t, Node[] nodeList)
 {
     for (int i = 0; i < nodeList.Length - 1; i++)
     {
         t.InsertNode(nodeList[i], nodeList[0]);
         ReassignHeightsAndBalanceFactors(nodeList);
         BalanceTreeIfUnbalanced(nodeList);
     }
 }
Exemplo n.º 3
0
 private static void ClearTreeByDeletion(Tree t, Node[] nodeList)
 {
     for (int i = 0; i <= nodeList.Length - 1; i++)
     {
         Console.WriteLine(nodeList[i].data);
         t.Delete(nodeList[i], nodeList);
         for (int j = 0; j < nodeList.Length; j++)
         //reassign heights and balance factors each time a node is inserted
         {
             nodeList[j].height = nodeList[j].Height(nodeList[j]);
             nodeList[j].balanceFactor = nodeList[j].BalanceFactor(nodeList[j].leftChild, nodeList[j].rightChild);
         }
         for (int j = 0; j < nodeList.Length; j++)
         {
             if (nodeList[j].balanceFactor == 2 || nodeList[j].balanceFactor == -2)
             {
                 BalanceTree(nodeList[j]);
                 //rebalance tree after each node is inserted if it is unbalanced
             }
         }
     }
 }