Java Project
Description
Unformatted Attachment Preview
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
public class BST {
Node root; // root of BST
BST() {
root = null;
}
Node insertRec(Node root, int data) {
// insert into tree
if (root == null) {
root = new Node(data);
return root;
}
if (data < root.data) // insert left child
root.left = insertRec(root.left, data);
else if (data > root.data) // insert right child
root.right = insertRec(root.right, data);
return root;
}
// helper functions
// call insertRec from main
void insert(int data) {
root = insertRec(root, data);
System.out.println(“New item inserted into tree => ” +
data);
void postorderRec(Node root) {
if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.println(root.data);
}
}
// Returns the max value in a binary tree
static int findMax(Node node) {
if (node == null)
return Integer.MIN_VALUE;
int res = node.data;
int lres = findMax(node.left);
int rres = findMax(node.right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
public static void main(String[] args) {
BST tree = new BST(); // create tree object
// build the tree
tree.root = new Node(2);
tree.root.left = new Node(7);
tree.root.right = new Node(5);
tree.root.left.right = new Node(6);
tree.root.left.right.left = new Node(1);
tree.root.left.right.right = new Node(11);
tree.root.right.right = new Node(9);
Purchase answer to see full
attachment
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."