Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA OR C++ Write a program implementing some operations on binary search trees.

ID: 652090 • Letter: J

Question

JAVA OR C++

Write a program implementing some operations on binary search trees. Requirements to the program:

The program should be menu-driven. The user will enter the code of operation. In the output, show that all functions work properly.

You can use the code of all tree functions from your textbook. In the beginning of the program, create an array of integers from the following list:  

30, 10, 45, 38, 20, 50, 25, 33, 8, 12 (in this particular order).

Create a binary search tree with nodes containing these numbers as key values

(here the function TREE-INSERT must be used).

Display the results of inorder, postorder and preorder traversals of your binary tree.

Show the sequences of the nodes when you search for the keys 38 and 9 (use the function TREE-SEARCH).

Delete the node with the key 10 (use the function TREE-DELETE).

Display the results of inorder, postorder and preorder traversals of your binary tree.

Explanation / Answer

public class Btree { Node root; Btree(){ this.root = null; } public static void inorderWalk(Node n){ if(n != null){ inorderWalk(n.left); System.out.print(n.value + " "); inorderWalk(n.right); } } public static Node getParent(Btree t, Node n){ Node current = t.root; Node parent = null; while(true){ if (current == null) return null; if( current.value == n.value ){ break; } if (current.value > n.value){ parent = current; current = current.left; } else{ //(current.value