Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework 1 : * * 2 5 . 3 ( Implement inorder traversal without using recursion ) Implement the inorder method in BST using a

Homework 1:
**25.3(Implement inorder traversal without using recursion) Implement the inorder
method in BST using a stack instead of recursion. Write a test program that
prompts the user to enter 15 integers, stores them in a BST, and invokes the
inorder method to display the elements.
Page
6
of 6
package chapter25;
import java.util.Scanner;
import chapter25.Exercise25_03.BST.TreeNode;
public class Exercise25_03HW {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
BST tree = new BST>();
// Prompt the user to enter 10 integers and store them in the tree
for (int i =0; i 10; i++){
tree.insert(input.nextInt());
}
tree.inorder();
tree.nonRecursiveInorder();
BST tree1= new BST>();
tree1.insert("George");
tree1.insert("Michael");
tree1.insert("Tom");
tree1.insert("Adam");
tree1.insert("Jones");
tree1.insert("Peter");
tree1.insert("John");
tree1.insert("Daniel");
tree1.inorder();
tree1.nonRecursiveInorder();
}
public static class BST> implements Tree {
protected TreeNode root;
protected int size =0;
/** Create a default binary search tree */
public BST(){
}
/** Create a binary search tree from an array of objects */
public BST(E[] objects){
for (int i =0; i objects.length; i++)
insert(objects[i]);
}
/** Return true if the element is in the tree */
public boolean search(E e){
TreeNode current = root; // Start from the root
while (current != null){
if (e.compareTo(current.element)0){
current = current.left;
} else if (e.compareTo(current.element)>0){
current = current.right;
} else
// element matches current.element
return true; // Element is found
}
return false;
}
/**
* Insert element e into the binary search tree Return true if the
element is
* inserted successfully
*/
public boolean insert(E e){
if (root == null)
root = createNewNode(e); // Create a new root
else {
// Locate the parent node
TreeNode parent = null;
TreeNode current = root;
while (current != null)
if (e.compareTo(current.element)0){
parent = current;
current = current.left;
} else if (e.compareTo(current.element)>0){
parent = current;
current = current.right;
} else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (e.compareTo(parent.element)0)
parent.left = createNewNode(e);
else
parent.right = createNewNode(e);
}
size++;
return true; // Element inserted
}
protected TreeNode createNewNode(E e){
return new TreeNode(e);
}
/** Inorder traversal from the root */
public void inorder(){
inorder(root);
}
/** Inorder traversal from a subtree */
protected void inorder(TreeNode root){
if (root == null)
return;
inorder(root.left);
System.out.print(root.element +"");
inorder(root.right);
}
/** Postorder traversal from the root */
public void postorder(){
postorder(root);
}
/** Postorder traversal from a subtree */
protected void postorder(TreeNode root){
if (root == null)
return;
postorder(root.left);
postorder(root.right);
System.out.print(root.element +"");
}
/** Preorder traversal from the root */
public void preorder(){
preorder(root);
}
/** Preorder traversal from a subtree */
protected void preorder(TreeNode root){
if (root == null)
return;
System.out.print(root.element +"");
preorder(root.left);
preorder(root.right);
}
/** Inner class tree node */
public static class TreeNode>{
E element;
TreeNode left;
TreeNode right;
public TreeNode(E e){
element = e;
}
}
/** Get the number of nodes in the tree */
public int getSize(){
return size;
}
/** Returns the root of the tree */
public TreeNode getRoot(){
return root;
}
/** Returns a path from the root leading to the specified element */
public java.util.ArrayList> path(E e){
java.util.ArrayList> list = new
java.util.ArrayList>();
TreeNode current = root; // Start from the root
while (current != null){
list.add(current); // Add the node to the list
if (e.compareTo(current.element)0){
current = current.left;
} else if (e.compareTo(current.element)>0){
current = current.right;
} else
break;
}
return list; // Return an array of nodes
}
/**
* Delete an element from the binary search tree. Return true if the
element is
* deleted successfully Return false if the element is not in the tree
*/
public boolean delete(E e){
// Locate the node to be deleted and also locate its parent node
TreeNode parent = null;
TreeNode current = root;
while (current != null){
if (e.compareTo(current.element)0){
parent = current;
current = current.left;
} else if (e.compareTo(current.element)>0){
parent = current;
current = current.right;
} else
break; // Element is in the tree pointed by current
}
if (current == null)
return false; // Element is not in the tree
// Case 1: current has no left children
if (current.left == null){
// Connect the parent with the right child of the current
node
if (parent == null){
root = current.right;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago