/// <summary> /// Inserts a new node into its sorted location the tree /// </summary> /// <param name="val">The value of the node to be added</param> public void Insert(int val) { //if the tree is empty, add the node as root if (Root == null) { Root = new IntNode(val); } //otherwise, find where to add it else { InsertNode(Root, val); } }
public void FizzBuzzRecursive(IntNode intNode, StringNode stringNode) { if (intNode.Left != null) { stringNode.Left = new StringNode(FizzBuzz(intNode.Left.Value)); FizzBuzzRecursive(intNode.Left, stringNode.Left); } else if (intNode.Right != null) { stringNode.Right = new StringNode(FizzBuzz(intNode.Right.Value)); FizzBuzzRecursive(intNode.Right, stringNode.Right); } }
/// <summary> /// Recursive helper method for pre-order traversal. Should not be called directly /// </summary> /// <param name="current">The current node being traversed</param> /// <param name="list">The list of values to be added to</param> public void PreOrderTraversal(IntNode current, List <int> list) { //add current, then go to left, then to right list.Add(current.Value); if (current.Left != null) { PreOrderTraversal(current.Left, list); } if (current.Right != null) { PreOrderTraversal(current.Right, list); } }
/// <summary> /// Recursive helper method for node insertion. Should not be called directly. /// </summary> /// <param name="current">The node to try an insertion on</param> /// <param name="val">The value of the node to be added</param> /// <returns></returns> public IntNode InsertNode(IntNode current, int val) { //if you've found a blank spot, add the value if (current == null) { current = new IntNode(val); } //go to the left if the value is smaller than current else if (current.Value > val) { current.Left = InsertNode(current.Left, val); } //go to the right if the value is larger than current else { current.Right = InsertNode(current.Right, val); } return(current); }
/// <summary> /// Recursive helper method for node search. Should not be called directly. /// </summary> /// <param name="current">The current node to be searched</param> /// <param name="val">The searched value</param> /// <returns></returns> public bool ContainsNode(IntNode current, int val) { //if you've found the value, you've found it if (current.Value == val) { return(true); } //if the value is smaller than the current, go to the left //skips if there is no left to go to else if (current.Value > val && current.Left != null) { return(ContainsNode(current.Left, val)); } //if the value is larger than the current, go to the right //skips if there is no right to go to else if (current.Right != null) { return(ContainsNode(current.Right, val)); } return(false); }
public BinaryIntTree() { Root = null; }
public IntNode(int val) { Value = val; Left = null; Right = null; }