Exemple #1
0
        // and now for a tree sort - I know I have spent to long on this but I was stuck writing the report/essay and this has got me interested
        public void TreeSort()
        {
            // start a new tree
            BinTree sortingTree = new BinTree(numbers[0], "tree sort");

            // go thought the array adding number to the tree -this will add the first number twice  but the tree will just not do anything but log it failed to add
            foreach (int number in numbers)
            {
                sortingTree.addNode(number, "tree sort");
            }
            // run the indexfrom lowest wich will go thouth the tree and get the indexes from the lowest to the highest - sorted. then go to the start of the link list
            dblLinked treeSorted = sortingTree.IndexFromLowest();

            treeSorted.goToStart();

            // the go right will skip the 1st so I will add that one manualy
            Debug.WriteLine(treeSorted.getData());
            numbers[0] = (int)treeSorted.getData();
            // start at one as the first has been done already
            int counter = 1;

            while (treeSorted.goRight())
            {
                numbers[counter] = (int)treeSorted.getData();
                counter++;
            }
        }
        // this is to check to see if a key exists
        public bool CheckKey(string key)
        {
            // lets get our list
            dblLinked bucket = storage[getHash(key)];

            // first lets see if there is any entry here
            if (!bucket.IsEmpty())
            {
                // get the start of the list
                bucket.goToStart();
                //check the first
                if ((string)((object[])bucket.getData())[0] == key)
                {
                    return(true);
                }
                // then loop and check the rest
                while (bucket.isRight())
                {
                    if ((string)((object[])bucket.getData())[0] == key)
                    {
                        return(true);
                    }
                    bucket.goRight();
                }
            }
            return(false);
        }
 // right side iteration button
 public void btnRight_click(object sender, EventArgs e)
 {
     //myTree.clearLog();
     //lblLog.Text = myTree.fromHighest().ToString();
     // creating the straight iteration list
     nodeList = myTree.preOrder();
     if (nodeList == null)
     {
         nodeList = new dblLinked();
     }                                                    // this should fix the app crashing when it gets a null list. and yeah the code is not well formatted but who cares
     nodeList.goToStart();
     lblLink.Text = nodeList.getData().ToString();
     //change the label above the link list display to say right side iteration
     lblIteration.Text = "Pre-order Iteration";
 }
 // straight ineration button
 public void btnStraight_click(object sender, EventArgs e)
 {
     // creating the straight iteration list
     nodeList = myTree.straightItr();
     if (nodeList == null)
     {
         nodeList = new dblLinked();
     }                                                    // this should fix the app crashing when it gets a null list. and yeah the code is not well formatted but who cares
     // go to the start of the list
     nodeList.goToStart();
     //display that list item in the label
     lblLink.Text = nodeList.getData().ToString();
     //change the label above the link list display to say straight iteration
     lblIteration.Text = "Straight Iteration";
 }
        public void addEntry(string key, object data)
        {
            int hashedKey = getHash(key);

            Debug.WriteLine(hashedKey + " =============== ");
            if (storage[hashedKey] == null)
            {
                storage[hashedKey] = new dblLinked(new object[] { key, data });
            }
            else
            {
                storage[hashedKey].addRight(new object[] { key, data });
            }
            hashedKeys.Add(hashedKey);
            keys.Add(key);
        }
        // ###################################### Straight Iteration
        // this is a left first straight iteration otherwise known as a level order traversal
        public dblLinked straightItr()
        {
            // check that the trunk exists before doing anything
            if (trunk != null)
            {
                dblLinked straightList = new dblLinked(trunk);
                // if I dont add a least one more node to the list
                // the loop will end at the start

                /*
                 * if(trunk.left!= null)
                 * {
                 *  straightList.addToEnd(trunk.left);
                 * }
                 * if (trunk.right != null)
                 * {
                 *  straightList.addToEnd(trunk.right);
                 * }
                 * /*  it looks like a do while works instead of a while loop*/
                do
                {
                    // ok so to make the line of code readablely short lets pull the current
                    // node from the list with a name
                    TreeNode tempTreeNode = (TreeNode)straightList.getData();
                    // if it has a left node add it to list
                    if (tempTreeNode.left != null)
                    {
                        straightList.addToEnd(tempTreeNode.left);
                    }
                    // ditto for right
                    if (tempTreeNode.right != null)
                    {
                        straightList.addToEnd(tempTreeNode.right);
                    }
                    // go to the next node in the list
                    //straightList.goRight();
                } while (straightList.goRight());
                // well thats it for the loop
                // it should should get though them all as it will stopp adding more
                // at the leaf nodes
                return(straightList);
            }
            else
            {
                return(null);
            }
        }
        public TreeAndList()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);


            // lests setup some random nodes
            Random rnd = new Random();

            for (int i = 0; i < 1; i++)
            {
                int rnum = rnd.Next(1, 1001);
                myTree.addNode(rnum, "random node");
                Debug.WriteLine("--== " + rnum + "==--");
                //nodeList.addToEnd(i + " added node"); // add a node to a test dbl link list
            }
            // now lets clear to log to make it easier to see wht I do
            myTree.clearLog();
            // creating the straight iteration list
            nodeList = myTree.straightItr();
            nodeList.goToStart();
            lblLink.Text = nodeList.getData().ToString();
            Debug.WriteLine("--== " + "dsfgdsghdfsghdfgjdfglhkjdshflghdslkfghjdslfkjghdfslkjg;ldfgj;ldsfhgldsjfhg;lnfghjdfslkjghlsdjfghldsjfg" + "==--");
        }
 // getting just the indexes in order - for reasons
 // left side iteration or list from lowest to highest
 private dblLinked indexLeftSideItr(TreeNode checkIt, bool fromChild, dblLinked results)
 {
     if (fromChild)
     {
         if (checkIt.right == null)
         {
             results.addToEnd(checkIt.sortingNum);
         }
         if (checkIt.parent != null)
         {
             if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum)
             {
                 results.addToEnd(checkIt.parent.sortingNum);
                 return(indexLeftSideItr(checkIt.parent.right, false, results));
             }
             else
             {
                 return(indexLeftSideItr(checkIt.parent, true, results));
             }
         }
         else
         {
             return(results);
         }
     }
     // ok so that is if its coming from a kid now if it coming from anywhere else
     else
     {
         if (checkIt.left != null)
         {
             return(indexLeftSideItr(checkIt.left, false, results));
         }
         else
         {
             results.addToEnd(checkIt.sortingNum);
             if (checkIt.right != null)
             {
                 return(indexLeftSideItr(checkIt.right, false, results));
             }
             else
             {
                 if (checkIt.parent != null)
                 {
                     // I forgot to check for a sister
                     if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum)
                     {
                         results.addToEnd(checkIt.parent.sortingNum);
                         return(indexLeftSideItr(checkIt.parent.right, false, results));
                     }
                     else
                     {
                         return(indexLeftSideItr(checkIt.parent, true, results));
                     }
                 }
                 else
                 {
                     return(results);
                 }
             }
         }
     }
     //return results;
 }
 // left side iteration or list from lowest to highest recording any time we dont go up the tree otherwise known as pre order traversal
 public dblLinked preOrderItr(TreeNode checkIt, bool fromChild, dblLinked results)
 {
     if (fromChild)
     {
         /*
          * if (checkIt.right == null)
          * {
          *  results.addToEnd(checkIt.ToString());
          * }*/
         // parent null means we are back at the trunk
         if (checkIt.parent != null)
         {
             // if its coming from the left go parent right(sister) if possible otherwise go up
             if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum)
             {
                 // results.addToEnd(checkIt.parent.ToString());
                 return(preOrderItr(checkIt.parent.right, false, results));
             }
             else
             {
                 return(preOrderItr(checkIt.parent, true, results));
             }
         }
         else
         {
             // back to trunk so now its over
             return(results);
         }
     }
     // ok so that is if its coming from a kid now if it coming from anywhere else
     else
     {
         // just record as long as we are not going back up
         results.addToEnd(checkIt.ToString());
         // first try to go down left(son) if not try right(daughter)
         if (checkIt.left != null)
         {
             return(preOrderItr(checkIt.left, false, results));
         }
         else
         {
             // results.addToEnd(checkIt.ToString());
             //well there was no son(left) so lets see if there was a daughter(right)
             if (checkIt.right != null)
             {
                 return(preOrderItr(checkIt.right, false, results));
             }
             else
             {
                 // no left(son) or right(daughter) so if there is no parent right(sister) to to the parent
                 if (checkIt.parent != null)
                 {
                     // I forgot to check for a sister
                     if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum)
                     {
                         // results.addToEnd(checkIt.parent.ToString());
                         return(preOrderItr(checkIt.parent.right, false, results));
                     }
                     else
                     {
                         return(preOrderItr(checkIt.parent, true, results));
                     }
                 }
                 else
                 {
                     // no son, daughter, or parent so we are alone at the trunk and can end
                     return(results);
                 }
             }
         }
     }
     //return results;
 }
 // ##########################################################
 // right side iteration or list from Highest to lowest
 public dblLinked rightSideItr(TreeNode checkIt, bool fromChild, dblLinked results)
 {
     if (fromChild)
     {
         if (checkIt.left == null)
         {
             // results += checkIt.ToString()  + "\r\n";
             results.addToEnd(checkIt);
         }
         if (checkIt.parent != null)
         {
             if (checkIt.parent.left != null && checkIt.sortingNum > checkIt.parent.sortingNum)
             {
                 // results += checkIt.parent.ToString() + "\r\n";
                 results.addToEnd(checkIt.parent);
                 return(rightSideItr(checkIt.parent.left, false, results));
             }
             else
             {
                 return(rightSideItr(checkIt.parent, true, results));
             }
         }
         else
         {
             return(results);
         }
     }
     // ok so that is if its coming from a kid now if it coming from anywhere else
     else
     {
         if (checkIt.right != null)
         {
             return(rightSideItr(checkIt.right, false, results));
         }
         else
         {
             //  results += checkIt.ToString() + "\r\n";
             results.addToEnd(checkIt);
             if (checkIt.left != null)
             {
                 return(rightSideItr(checkIt.left, false, results));
             }
             else
             {
                 if (checkIt.parent != null)
                 {
                     // I forgot to check for a sister
                     if (checkIt.parent.left != null && checkIt.sortingNum > checkIt.parent.sortingNum)
                     {
                         // results += checkIt.parent.ToString()  + "\r\n";
                         results.addToEnd(checkIt.parent);
                         return(rightSideItr(checkIt.parent.left, false, results));
                     }
                     else
                     {
                         return(rightSideItr(checkIt.parent, true, results));
                     }
                 }
                 else
                 {
                     return(results);
                 }
             }
         }
     }
     //return results;
 }