/// <summary> /// Try to add a node to the current node. /// </summary> /// <param name="node"></param> public void AddNode(NodeString node) { //If the new node's value is less than the current node's value. if (String.Compare(node.value, this.value) < 0) { //If the current node dosnt have a left node. if (this.left == null) { //Set the child to the new node. this.left = node; } else { //Try to add the new node to the child. this.left.AddNode(node); } } //If the new node's value is more than the current node's value. else if (String.Compare(node.value, this.value) > 0) { //If the current node dosnt have a right node. if (this.right == null) { //Set the child to the new node. this.right = node; } else { //Try to add the new node to the child. this.right.AddNode(node); } } }
private static void CreateNode(BinaryTree <NodeString> tree, string afterID, NodeString newNode, ATCHILDNODE child) { //Console.WriteLine("Finding {0} to place {1}", afterID,newNode.ID); var visited = new Stack <BinaryTreeNode <NodeString> >(); visited.Push(tree.Root); BinaryTreeNode <NodeString> node = findNode(tree.Root, visited, afterID); if (node != null) { switch (child) { case ATCHILDNODE.LEFT: //Console.WriteLine("Placed {0} Left of {1}", newNode.ID,node.Value.ID); node.Left = new BinaryTreeNode <NodeString>(newNode); break; case ATCHILDNODE.RIGHT: //Console.WriteLine("Placed {0} right of {1}", newNode.ID, node.Value.ID); node.Right = new BinaryTreeNode <NodeString>(newNode); break; } } //else { Console.WriteLine("Node {0} not found", afterID); } }
/// <summary> /// Search through the tree. /// </summary> /// <param name="val">The value to search for.</param> /// <returns>The found node.</returns> public NodeString Search(string val) { //Start the search at the root. NodeString found = root.Search(val); //Return what we found. return(found); }
/// <summary> /// Add a value to the tree. /// </summary> /// <param name="val"></param> public void AddValue(string val) { //Create a new node. NodeString node = new NodeString(this, val); //If there isnt a root node. if (this.root == null) { //Set the new node as the root node. this.root = node; } else { //Try to add the new node to the root. this.root.AddNode(node); } }
public void InsertAfter(NodeString node, BinaryTree <NodeString> tree) { }
//Dose that thing. static public void StringSearch() { //Welcome the user. Console.WriteLine("Welcome to the String Binary Search Tree."); Console.WriteLine(); //Create the Binary Tree. BinaryTreeString tree = new BinaryTreeString(); //Create a Random number. Random rand = new Random(); //Print to the console. Console.WriteLine("Here is 10 names:"); //List of names List <string> names = new List <string>(); names.Add("Nathan"); names.Add("Jacob"); names.Add("Rosie"); names.Add("Tammy"); names.Add("Tom"); names.Add("Judy"); names.Add("Milly"); names.Add("Craig"); names.Add("Matt"); names.Add("Lucy"); names.Add("Zack"); names.Add("Casey"); names.Add("David"); //Get 10 random numbers. for (int i = 0; i < names.Count; i++) { //Add the number to the Binary Tree. tree.AddValue(names[i]); //Print the number to the console. Console.Write(names[i]); //Insert the commas. if (i < (names.Count - 1)) { Console.Write(","); } } //Output to the console. Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Here they are sorted:"); //Traverse the tree. tree.Traverse(); tree.Print(); Console.Write("\b \b"); //Output to the console. Console.WriteLine(); Console.WriteLine(); Console.Write("Enter number to search for: "); //Get the input to search. string input = Console.ReadLine(); //Search the Binary Tree. NodeString search = tree.Search(input); //New Line. Console.WriteLine(); //If the returned node isnt null. if (search != null) { //We found it. Console.WriteLine("Found: " + search.value + " :) \n"); } else { //Not found. Console.WriteLine("Not Found :( \n"); } //Print to the console. Console.Write("Press enter to restart..."); //Pause. Console.ReadLine(); //Clear the console. Console.Clear(); //Restart the Console App. StringSearch(); }
/// <summary> /// Constructor. /// </summary> public BinaryTreeString() { //Set the root to null. this.root = null; }