/// <summary> /// counts the height of the tree recursiveley /// </summary> /// <param name="node"></param> /// <returns>int height</returns> public int height(IntNode node) { var result = 0; if (node != null) { result = Math.Max(height(node.leftLeaf), height(node.rightLeaf)) + 1; } return(result); }
/// <summary> /// Prints the data of the node /// </summary> /// <param name="n"></param> public void display(IntNode n) { if (n == null) { return; } display(n.leftLeaf); Console.Write(" " + n.number); display(n.rightLeaf); }
/// <summary> /// Inserts new data into the tree /// </summary> /// <param name="d"></param> public void insert(int d) { if (isEmpty()) { root = new IntNode(d); } else { root.insertData(ref root, d); } count++; }
/// <summary> /// takes in a node and data to be inserted and then inserts the node into /// the proper location based off the data. /// </summary> /// <param name="node"></param> /// <param name="data"></param> public void insertData(ref IntNode node, int data) { if (node == null) { node = new IntNode(data); } else if (node.number < data) { insertData(ref node.rightLeaf, data); } else if (node.number > data) { insertData(ref node.leftLeaf, data); } }
public IntNode(int value) { number = value; rightLeaf = null; leftLeaf = null; }
public IntBinaryTree() { root = null; count = 0; }