Question: Write a UML class diagram for this program. //BinTree.java package com.test; /* Class containing left and right child of current node and key value*/ class
Write a UML class diagram for this program.
//BinTree.java
package com.test;
/* Class containing left and right child of current node and key value*/
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
// Java program to demonstrate insert and treeWalk operation in binary search tree
public class BinTree {
// Root of BST
Node root;
// Constructor
BinTree() {
root = null;
}
// This method mainly calls insertRec()
void insertNode (Node n) {
root = insertRec(root, n);
}
/* A recursive function to insert a new key in BST */
Node insertRec(Node root, Node n) {
/* If the tree is empty, return a new node */
if (root == null) {
root = n;
return root;
}
/* Otherwise, recur down the tree */
if (n.key < root.key)
root.left = insertRec(root.left, n);
else if (n.key > root.key)
root.right = insertRec(root.right, n);
/* return the (unchanged) node pointer */
return root;
}
// This method mainly calls InorderRec()
void treeWalk() {
treeWalkRec(root);
}
// A utility function to do inorder traversal of BST
void treeWalkRec(Node root) {
if (root != null) {
treeWalkRec(root.left);
System.out.println(root.key);
treeWalkRec(root.right);
}
}
// Driver Program to test above functions
public static void main(String[] args) {
// testcase 2
System.out.println();
System.out.println("testcase 1:");
BinTree binTree2 = new BinTree();
binTree2.insertNode(new Node(3));
binTree2.insertNode(new Node(201));
binTree2.insertNode(new Node(60));
binTree2.insertNode(new Node(30));
binTree2.insertNode(new Node(45));
binTree2.treeWalk();
// testcase 3
System.out.println();
System.out.println("testcase 2_1:");
BinTree binTree3 = new BinTree();
binTree3.insertNode(new Node(-10));
binTree3.insertNode(new Node(-150));
binTree3.insertNode(new Node(4));
binTree3.insertNode(new Node(300));
binTree3.insertNode(new Node(45));
binTree3.treeWalk();
binTree3.insertNode(new Node(-50));
binTree3.insertNode(new Node(200));
System.out.println();
System.out.println("testcase 2_2:");
binTree3.treeWalk();
}
}
//output:
testcase 1: 3 30 45 60 201
testcase 2_1: -150 -10 4 45 300
testcase 2_2: -150 -50 -10 4 45 200 300
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
