Esempio n. 1
0
        //METHODS***********************************************************************

        /// <summary>
        /// Will either fill or send a variable to either the lesser or greater child nodes
        /// </summary>
        /// <param name="newVar"></param>
        public void InsertNode(int newVar)
        {
            if (newVar < var)                       //if insertedNode is less then the current variable...
            {
                if (lesserChild == null)            //and lesserChild is empty...
                {
                    lesserChild = new Node(newVar); //create new node at lesser child position with newVariable...

                    return;
                }
                else
                {
                    lesserChild.InsertNode(newVar);//and if there is something in lesserchild, send this data to the lesser child
                    return;
                }
            }
            else if (newVar > var)//same thing here but with greater child
            {
                if (greaterChild == null)
                {
                    greaterChild = new Node(newVar);
                    return;
                }
                else
                {
                    greaterChild.InsertNode(newVar);

                    return;
                }
            }
            else
            {
                throw new Exception("This tree only accepts unique values");
            }
        }
Esempio n. 2
0
        public void TreeRunThrough()
        {
            Node root = new Node(5);

            root.InsertNode(4);
            root.InsertNode(2);
            root.InsertNode(6);
            root.InsertNode(9);
            root.InsertNode(1);
            root.InsertNode(7);

            root.SearchBST(1);
        }