Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Homework 1 : * * 2 5 . 3 ( Implement inorder traversal without using recursion ) Implement the inorder method in BST using a
Homework :
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 integers, stores them in a BST and invokes the
inorder method to display the elements.
Page
of
package chapter;
import java.util.Scanner;
import chapterExerciseBSTTreeNode;
public class ExerciseHW
public static void mainString args
Scanner input new ScannerSystemin;
BST tree new BST;
Prompt the user to enter integers and store them in the tree
for int i ; i ; i
tree.insertinputnextInt;
tree.inorder;
tree.nonRecursiveInorder;
BST tree new BST;
treeinsertGeorge;
treeinsertMichael;
treeinsertTom;
treeinsertAdam;
treeinsertJones;
treeinsertPeter;
treeinsertJohn;
treeinsertDaniel;
treeinorder;
treenonRecursiveInorder;
public static class BST implements Tree
protected TreeNode root;
protected int size ;
Create a default binary search tree
public BST
Create a binary search tree from an array of objects
public BSTE objects
for int i ; i objects.length; i
insertobjectsi;
Return true if the element is in the tree
public boolean searchE e
TreeNode current root; Start from the root
while current null
if ecompareTocurrentelement
current current.left;
else if ecompareTocurrentelement
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 insertE e
if root null
root createNewNodee; Create a new root
else
Locate the parent node
TreeNode parent null;
TreeNode current root;
while current null
if ecompareTocurrentelement
parent current;
current current.left;
else if ecompareTocurrentelement
parent current;
current current.right;
else
return false; Duplicate node not inserted
Create the new node and attach it to the parent node
if ecompareToparentelement
parent.left createNewNodee;
else
parent.right createNewNodee;
size;
return true; Element inserted
protected TreeNode createNewNodeE e
return new TreeNodee;
Inorder traversal from the root
public void inorder
inorderroot;
Inorder traversal from a subtree
protected void inorderTreeNode root
if root null
return;
inorderrootleft;
System.out.printrootelement ;
inorderrootright;
Postorder traversal from the root
public void postorder
postorderroot;
Postorder traversal from a subtree
protected void postorderTreeNode root
if root null
return;
postorderrootleft;
postorderrootright;
System.out.printrootelement ;
Preorder traversal from the root
public void preorder
preorderroot;
Preorder traversal from a subtree
protected void preorderTreeNode root
if root null
return;
System.out.printrootelement ;
preorderrootleft;
preorderrootright;
Inner class tree node
public static class TreeNode
E element;
TreeNode left;
TreeNode right;
public TreeNodeE 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 pathE e
java.util.ArrayList list new
java.util.ArrayList;
TreeNode current root; Start from the root
while current null
list.addcurrent; Add the node to the list
if ecompareTocurrentelement
current current.left;
else if ecompareTocurrentelement
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 deleteE e
Locate the node to be deleted and also locate its parent node
TreeNode parent null;
TreeNode current root;
while current null
if ecompareTocurrentelement
parent current;
current current.left;
else if ecompareTocurrentelement
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 : current has no left children
if currentleft null
Connect the parent with the right child of the current
node
if parent null
root current.right;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started