void insert(NodeB root, int key) { if (root == null) { NodeB node = new NodeB(key); this.root = node; } else if (key < root.data) { if (root.left != null) { insert(root.left, key); } else { root.left = new NodeB(key); } } else if (key > root.data) { if (root.right != null) { insert(root.right, key); } else { root.right = new NodeB(key); } } }
void printInorder(NodeB NodeB) { if (NodeB == null) { return; } printInorder(NodeB.left); Console.Write(NodeB.data + " "); printInorder(NodeB.right); }
void printPreorder(NodeB node) { if (node == null) { return; } Console.Write(node.data + "-"); printPreorder(node.left); printPreorder(node.right); }
int minKey(NodeB root) { int min = root.data; while (root != null) { min = root.data; root = root.left; } return(min); }
int maxKey(NodeB root) { int max = root.data; while (root != null) { max = root.data; root = root.right; } return(max); }
NodeB searchRec(NodeB root, int key) { if (root == null || root.data == key) { return(root); } if (root.data > key) { return(searchRec(root.left, key)); } return(searchRec(root.right, key)); }
bool isBST(NodeB node, int min, int max) { /* an empty tree is BST */ if (node == null) { return(true); } if (node.data < min || node.data > max) { return(false); } return(isBST(node.left, min, node.data - 1) && isBST(node.right, node.data + 1, max)); }
bool search(NodeB root, int key) { while (root != null) { Console.Write(root.data + "-"); if (key > root.data) { root = root.right; } else if (key < root.data) { root = root.left; } else { return(true); } } return(false); }
public BinaryTree() { root = null; }
public NodeB(int item) { data = item; left = right = null; }