Example #1
0
        /// <summary>
        /// Pre order tree method - overload
        /// </summary>
        /// <param name="root">root node</param>
        /// <param name="list">pre order list of nodes</param>
        /// <returns></returns>
        public static List <int> PeOrder(Nodeb root, List <int> list)
        {
            try
            {
                if (root == null)
                {
                    return(null);
                }

                list.Add(root.Value);
                if (root.Left != null)
                {
                    PeOrder(root.Left, list);
                }

                if (root.Right != null)
                {
                    PeOrder(root.Right, list);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(list);
        }
        /// <summary>
        /// Initial Add attempt method
        /// </summary>
        /// <param name="value"></param>
        public void Add2(int value)
        {
            Nodeb newNode = new Nodeb(value);
            Nodeb Root    = newNode;

            if (Root == null)
            {
                Root = newNode;
            }
            else
            {
                Nodeb current = Root;
                Nodeb parent;
                parent = current;
                while (true)
                {
                    if (value < current.Value)
                    {
                        if (parent.Left == null)
                        {
                            parent.Left = newNode;
                            return;
                        }
                        else
                        {
                            parent = parent.Left;
                        }
                    }
                    else
                    {
                        if (parent.Right == null)
                        {
                            parent.Right = newNode;
                            return;
                        }
                        else
                        {
                            parent = parent.Right;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Searches tree for specific node
        /// </summary>
        /// <param name="value">value of node to search</param>
        /// <returns>true if it contains, false if not in tree</returns>
        public bool Contain(int value)
        {
            Nodeb newNode = new Nodeb(value);
            Nodeb root    = newNode;

            while (root != null)
            {
                if (root.Value == value)
                {
                    return(true);
                }
                else if (root.Value > value)
                {
                    root = root.Left;
                }
                else
                {
                    root = root.Right;
                }
            }
            return(false);
        }
        /// <summary>
        /// Overloaded method to identify proper placedment for new node
        /// </summary>
        /// <param name="value">value to add</param>
        /// <param name="node">current node</param>
        /// <returns></returns>
        public Nodeb Add(int value, Nodeb node)
        {
            if (node == null) /// if null, add node to current position
            {
                return(new Nodeb(value));
            }

            if (node.Value > value)                /// if node is greater than the value, look to the left
            {
                node.Left = Add(value, node.Left); //recussive call
            }

            else if (node.Value < value)             /// if node is less than value, look to the right
            {
                node.Right = Add(value, node.Right); // recurssive call
            }

            else
            {
                return(node);
            }
            return(node);
        }
 /// <summary>
 /// Initial method call for add, uses recursion
 /// </summary>
 /// <param name="value">node to add</param>
 public void Add(int value)
 {
     Root = Add(value, Root);
 }
        public BTS(int value)
        {
            Nodeb newNode = new Nodeb(value);

            Root = newNode;
        }
 public BTS()
 {
     Root = null;
 }
Example #8
0
 public BT(Nodeb root)
 {
     Root  = root;
     Count = 0;
 }