コード例 #1
0
 private void Print(Binarynode p, int padding)
 {
     if (p != null)
     {
         if (p.right != null)
         {
             Print(p.right, padding + 4);
         }
         if (padding > 0)
         {
             Console.Write(" ".PadLeft(padding));
         }
         if (p.right != null)
         {
             Console.Write("/\n");
             Console.Write(" ".PadLeft(padding));
         }
         Console.Write(p.item.ToString() + "\n ");
         if (p.left != null)
         {
             Console.Write(" ".PadLeft(padding) + "\\\n");
             Print(p.left, padding + 4);
         }
     }
 }
コード例 #2
0
 /// <summary>
 ///  Add a symbol to the tree if it's a new one. Returns reference to the new
 ///  node if a new node inserted, else returns null to indicate node already present.
 ///  1. Starting at the root of the tree, search the binary tree comparing the new key to the key in the current node.
 ///  If the new key is less than the current node, search the left subtree. If the new key is greater than the current node, search the right subtree.
 ///  2. When there is no left (or right) child to search, we have found the position in the tree where the new node should be installed.
 ///  3. To add a node to the tree, create a new TreeNode object and insert the object at the point discovered in the previous step.
 /// </summary>
 /// <param name="Node">Name of node to add to tree</param>
 /// <param name="Item">Value of node</param>
 /// <returns> Returns reference to the new node is the node was inserted.
 /// Returns a default sort order comparer for the type specified by the generic argument.</returns>
 private bool Add_Sub(Binarynode Node, int Item)
 {
     if (_comparer.Compare(Node.item, Item) < 0)
     {
         if (Node.right == null)
         {
             Node.right = new Binarynode(Item);
             _count++;
             return(true);
         }
         else
         {
             return(Add_Sub(Node.right, Item));
         }
     }
     else if (_comparer.Compare(Node.item, Item) > 0)
     {
         if (Node.left == null)
         {
             Node.left = new Binarynode(Item);
             _count++;
             return(true);
         }
         else
         {
             return(Add_Sub(Node.left, Item));
         }
     }
     else
     {
         return(false);
     }
 }
コード例 #3
0
 /// <summary>
 /// create constructor for that
 /// Initially root is equal to null & ofcourse count is also null
 /// & allocate space for array
 /// then for loop for count the node one by one and sorted data.
 /// </summary>
 /// <param name="size"></param>
 public BinarySearch(int size)
 {
     _root  = null;
     _count = 0;
     data   = new int[size];
     for (int i = 0; i < size; i++)
     {
         data[i] = _count;
     }
     Array.Sort(data);
 }
コード例 #4
0
 /// <summary>
 /// Add method:
 /// if the root is null then create a new root and increase by one ans so on and if it is not return the add_sub method
 /// </summary>
 /// <param name="Item">passing item to be current node</param>
 /// <returns>Sucessfull</returns>
 public bool Add(int Item)
 {
     if (_root == null)
     {
         _root = new Binarynode(Item);
         _count++;
         return(true);
     }
     else
     {
         return(Add_Sub(_root, Item));
     }
 }
コード例 #5
0
        /// <summary>
        /// Find the next ordinal node starting at node startNode.
        /// Due to the structure of a binary search tree, the
        /// successor node is simply the left most node on the right branch.
        /// </summary>
        /// <param name="value">Name key to use for searching</param>
        /// <returns>Returns a reference to the node if successful, else null</returns>
        public int Search(int value)
        {
            int        found     = -1;
            Binarynode localRoot = _root;

            found = find(value, "left", localRoot);
            if (found == -1)
            {
                localRoot = _root;
                found     = find(value, "right", localRoot);
            }
            if (found == -1)
            {
                Console.WriteLine("Item {0} not found in tree.", value);
            }
            return(found);
        }
コード例 #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <param name="findSide"></param>
        /// <param name="localRoot"></param>
        /// <returns></returns>
        private int find(int value, string findSide, Binarynode localRoot)
        {
            int found = -1;

            //Binarynode localRoot = _root;
            while (localRoot != null)
            {
                if (value.Equals(localRoot.item))
                {
                    found = localRoot.item;
                    break;
                }
                else if (localRoot.left != null && value.Equals(localRoot.left.item))
                {
                    found = localRoot.left.item;
                    break;
                }
                else if (localRoot.right != null && value.Equals(localRoot.right.item))
                {
                    found = localRoot.right.item;
                    break;
                }
                if (findSide.Equals("left"))
                {
                    localRoot = localRoot.left;
                }
                else if (findSide.Equals("right"))
                {
                    localRoot = localRoot.right;
                }
            }

            if (found != -1)
            {
                Console.WriteLine("Item found in tree = {0} on side = {1}", found, findSide);
                return(found);
            }
            return(found);
        }
コード例 #7
0
 /// <summary>
 ///  Constructor to create a single node item as like root
 /// </summary>
 /// <param name="item">passing item parameters</param>
 public Binarynode(int item)
 {
     this.item = item;
     left      = null;
     right     = null;
 }