The following Program in java implements a BST. The BST node (TNode) contains a data part as well as two links to its right and left children.  1. Draw (using paper and pen) the BST that results from the insertion of the values 60,30, 20, 80, 15, 70, 90, 10, 25, 33 (in this order). These values are used by the program  2. Traverse the tree using preorder, inorder and postorder algorithms (using paper and pen)  3. Write the program in c++, follow its steps, then compile and run.  - Compare the results to what you obtained in (2) above.  - Try different values  class TestBST { public static void main(String []args) { int i; int x[] = {60,30, 20, 80, 15, 70, 90, 10, 25, 33}; BST t = new BST(); for (i=0; i < 10; i++) { System.out.print(" Adding: "+x[i]+" to the BST "); t.insert(x[i]); } System.out.println("\nPreorder traversal result:"); t.preorder(t.root); System.out.println("\nInorder traversal result:"); t.inorder(t.root); System.out.println("\nPostorder traversal result:"); t.postorder(t.root); } } class TNode { TNode() { data = 0; left = null; right = null;} TNode(int d) { data = d; left = null; right = null;} public int getData() { return data;} public TNode getRight() { return right; } public TNode getLeft() { return left; } public void setRight(TNode tn) { right = tn; } public void setLeft(TNode tn) { left = tn; } private int data; private TNode right = null, left = null; } class BST { public BST() { root = null;} public boolean empty() { return (root == null); } public int getRoot() { return root.getData();} public void insert(int d) { TNode newNode = new TNode(d); if (empty()) { root = newNode; return; } TNode temp = root, parent = root; int direction = 0; // 0 left 1 right while (temp != null) { parent = temp; if (d > temp.getData()) { temp = temp.getRight(); direction = 1; } else { temp = temp.getLeft(); direction = 0; } } if (direction == 0) parent.setLeft(newNode); else parent.setRight(newNode); } void visit(TNode t) { System.out.print(" "+t.getData()+" "); } void inorder (TNode r) //yields ordered sequence { if (r !=null) { inorder(r.getLeft()); // L visit(r); // V inorder(r.getRight()); // R } } void preorder(TNode r) { if (r != null) { visit(r); // V preorder(r.getLeft()); // L preorder(r.getRight()); // R } } void postorder(TNode r) { if (r != null) { postorder(r.getLeft()); // L postorder(r.getRight()); // R visit (r); // V } } public TNode root = null ; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

The following Program in java implements a BST. The BST node (TNode) contains a data part as well as two links to its right and left children. 

1. Draw (using paper and pen) the BST that results from the insertion of the values 60,30, 20, 80, 15, 70, 90, 10, 25, 33 (in this order). These values are used by the program 
2. Traverse the tree using preorder, inorder and postorder algorithms (using paper and pen) 
3. Write the program in c++, follow its steps, then compile and run. 
- Compare the results to what you obtained in (2) above. 
- Try different values 


class TestBST
{
public static void main(String []args)
{
int i;
int x[] = {60,30, 20, 80, 15, 70, 90, 10, 25, 33};
BST t = new BST();
for (i=0; i < 10; i++)
{
System.out.print(" Adding: "+x[i]+" to the BST ");
t.insert(x[i]);
}
System.out.println("\nPreorder traversal result:");
t.preorder(t.root);
System.out.println("\nInorder traversal result:");
t.inorder(t.root);
System.out.println("\nPostorder traversal result:");
t.postorder(t.root);
}
}
class TNode
{
TNode()
{ data = 0; left = null; right = null;}
TNode(int d)
{ data = d; left = null; right = null;}
public int getData()
{ return data;}
public TNode getRight()
{ return right; }
public TNode getLeft()
{ return left; }
public void setRight(TNode tn)
{ right = tn; }
public void setLeft(TNode tn)
{ left = tn; }
private int data;
private TNode right = null, left = null;
}
class BST
{
public BST() { root = null;}
public boolean empty() { return (root == null); }
public int getRoot() { return root.getData();}
public void insert(int d)
{
TNode newNode = new TNode(d);
if (empty())
{
root = newNode;
return;
}
TNode temp = root, parent = root;
int direction = 0; // 0 left 1 right
while (temp != null)
{
parent = temp;
if (d > temp.getData())
{
temp = temp.getRight();
direction = 1;
}
else
{
temp = temp.getLeft();
direction = 0;
}
}
if (direction == 0)
parent.setLeft(newNode);
else
parent.setRight(newNode);
}
void visit(TNode t)
{
System.out.print(" "+t.getData()+" ");
}
void inorder (TNode r) //yields ordered sequence
{
if (r !=null)
{
inorder(r.getLeft()); // L
visit(r); // V
inorder(r.getRight()); // R
}
}
void preorder(TNode r)
{
if (r != null)
{
visit(r); // V
preorder(r.getLeft()); // L
preorder(r.getRight()); // R
}
}
void postorder(TNode r)
{
if (r != null)
{
postorder(r.getLeft()); // L
postorder(r.getRight()); // R
visit (r); // V
}
}
public TNode root = null ;
}

 

Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY