示例#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);
        }
        // the save button
        public void btnSave_click(object sender, EventArgs e)
        {
            TreeNode foundNode = myTree.findNode(int.Parse(txtKey.Text));

            if (foundNode != null)
            {
                DisplayAlert("node Saved", foundNode.ToString(), "Okay");
                saveList.addRight(foundNode);
                lblSaveList.Text = saveList.getData().ToString();
            }
            else
            {
                DisplayAlert("cant save", "no node found", "Okay");
            }
        }
        // #################################### iteration buttons #####################
        // left side iteration button
        public void btnLeft_click(object sender, EventArgs e)
        {
            //  myTree.clearLog();
            // lblLog.Text = myTree.fromLowest();
            // creating the straight iteration list

            nodeList = myTree.fromLowest();
            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 = "Left Side Iteration";
        }
        // ###################################### 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" + "==--");
        }