Question

Java: Create a BST with input keys S E X R A C. (Assume the values...

Java:

Create a BST with input keys S E X R A C. (Assume the values are 1,2,3,4,5,6, resp.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

========================

JAVA PROGRAM

========================

///////////////BST.java/////////////////

//Class: BST

public class BST {

/**

*inner class Node

*/

class Node {

String key;

int value;

Node left;

Node right;

//constructor

public Node(String key,int value){

this.key = key;

this.value = value;

}

@Override

public String toString() {

return "Node [key=" + key + ", value=" + value + "]";

}

}

private Node root;

/**

* public method insert

* It will call insertKey method

* by passing root node along with key and value

* @param key

* @param value

*/

public void insert(String key,int value){

root = insertKey(root,key,value);

}

/**

* private recursive method insertKey

* It will insert the node at its proper position

* in binary search tree

* @param node

* @param key

* @param value

* @return

*/

private Node insertKey(Node node, String key,int value){

if(node == null){//if node is null

return new Node(key,value);//return a new Node

}

//compare current key , with key of current node

int compareValue = key.compareTo(node.key);

if(compareValue < 0)//if it is lesser

node.left = insertKey(node.left,key,value);//insert key in left child

else if(compareValue > 0)//if it is greater

node.right = insertKey(node.right,key,value);//insert key at right child

else //if it is same

node.value = value;//update the value

return node;

}

/**

* public method printTree

* It will call private method print

* by passing root Node

*/

public void printTree(){

System.out.println("Traversing Binary search Tree In-order:");

print(root);

}

/**

* print tree with in-order traversal

* this is a recursive method

* @param node

*/

private void print(Node node){

if(node ==null)

return;

//print inOrder

print(node.left);//left child

System.out.println(node);//print node

print(node.right);//right child

}

}

/////////////////////TestBST.java//////////////////////

//Test class: TestBST

public class TestBST{

//main method

public static void main(String[] args){

BST bst = new BST();//create a new instance of BST

//insert keys as given with their values

bst.insert("S", 1);

bst.insert("E", 2);

bst.insert("X", 3);

bst.insert("R", 4);

bst.insert("A", 5);

bst.insert("C", 6);

//finally print the Binary search tree with

//inserted nodes in-order

bst.printTree();

}

}

================================

OUTPUT

================================

Traversing Binary search Tree In-order:

Node [key=A, value=5]

Node [key=C, value=6]

Node [key=E, value=2]

Node [key=R, value=4]

Node [key=S, value=1]

Node [key=X, value=3]

Add a comment
Know the answer?
Add Answer to:
Java: Create a BST with input keys S E X R A C. (Assume the values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work...

    JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work with “big” numbers.) Create a completely-skewed BST S containing 1, 2, . . . , x. Create a BST R containing x integers without repetitions gen- erated at random. (To minimize the risk of repetitions, you can multiply the value returned by random() by a big number.) Given that the numbers are generated uniformly at random, the tree will likely be balanced. Measure the...

  • 1a) Draw the 2-3 tree that results when you insert the keys S E A R...

    1a) Draw the 2-3 tree that results when you insert the keys S E A R C H X M P L Y in that order into an initially empty tree. 1b) Construct the corresponding left leaning red-black tree from part a. 1c) Find a sequence of keys to insert into a BST and a left leaning red-black BST such that the height of the BST is less than the height of the left leaning red-black BST, or prove that...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

  • Q8 BST 15 Points Given a BST T with root r write algorithms (pseudocode) to determine:...

    Q8 BST 15 Points Given a BST T with root r write algorithms (pseudocode) to determine: (a) The height of T. (b) The maximum element in T. (c) If T is height balanced. Please select file(s) Select file(s) Q9 Double 15 Points Consider inserting the keys 10, 22, 31, 4, 15, 28, 17, 88,59 into a hash tabl- (1) lend

  • Assume that R(A, B, C, D, E, F) has been decomposed into S(A, C, E, F)...

    Assume that R(A, B, C, D, E, F) has been decomposed into S(A, C, E, F) and other relations. If the dependencies for R are: AB rightarrow C, C rightarrow E, E rightarrow D, D rightarrow F, F rightarrow D. (a) Find ALL non-trivial functional dependencies that hold in S (b) Determine the keys and superkeys of S (c) For each one of your functional dependencies from part a) indicate if it is a BCNF violation, a 3NF violation or...

  • package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values...

    package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...

  • Consider the relation R(A, B, C, D, E), where it is known that the only keys...

    Consider the relation R(A, B, C, D, E), where it is known that the only keys are {A, C, D} and {D, E}: Give a set of functional dependencies that will make {A, C, D} and {D, E} be the only keys of R. This set should be such that if you delete any FD, then the keys of R will be something other than {A, C, D} and {D, E}.

  • part C please 8 BST 15 Points Given a BST T with root r write algorithms...

    part C please 8 BST 15 Points Given a BST T with root r write algorithms (pseudocode) to determine: (a) The height of T. (b) The maximum element in T. (c) If T is height balanced. Please select file(s) Select file(s) Q9 Double

  • 1. Recall that x E R is positive (resp. negative) if x = (an) which is...

    1. Recall that x E R is positive (resp. negative) if x = (an) which is positively (resp textitnegatively) bounded away from 0. Prove the following LIM00 an for a Cauchy sequence n-oo (a) For any E R, exactly one of the following is true: x is positive, is negative, or x= 0 E R is positive if and only if -x is negative. (b) (c) If x, y E R are both positive, then x + y and xy...

  • #1 Create two AVL trees with 7 keys: A, B, C, D, E, F, and G....

    #1 Create two AVL trees with 7 keys: A, B, C, D, E, F, and G. (There are 5,040 different key sequences that produce 429 unique trees. Of those trees, 17 are AVL trees.) a. Draw the shortest AVL tree containing these 7 keys. b. Draw one of the tallest possible AVL trees containing these 7 keys.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT