Question

Question - modify the code below so that for a node, the value of every node...

Question

- modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node.
- create such a binary tree in the Main method, and call the following method:

  •  InOrder(Node theRoot),

  •  PreOrder(Node theRoot),

  •  PostOrder(Node theRoot),

  •  FindMin(),

  •  FindMax(),

  •  Find(int key)

    using System;
    using System.Collections.Generic; using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication38 {

    public class Node {

    public int Data;
    public Node Left;
    public Node Right;
    public void DisplayNode() {

    Console.Write(Data + " "); }

    }

    public class BinarySearchTree {

    public Node root;
    public BinarySearchTree() {

    root = null; }

    public void Insert(int i) {

    Node newNode = new Node(); newNode.Data = i;

    if (root == null) {

root = newNode;

} else {

Node current = root; Node parent;
while (true)
{

parent = current;
if (i < current.Data) {

current = current.Left; if (current == null)
{

parent.Left = newNode;

break; }

} else {

break; }

}
}//end of while(true)

}//end of else
}//end of public void Insert(int i)

public void InOrder(Node theRoot) {

if (!(theRoot == null)) {

InOrder(theRoot.Left); theRoot.DisplayNode(); InOrder(theRoot.Right);

} }

public void PreOrder(Node theRoot) {

if (!(theRoot == null)) {

theRoot.DisplayNode(); PreOrder(theRoot.Left); PreOrder(theRoot.Right);

} }

public void PostOrder(Node theRoot) {

if (!(theRoot == null)) {

PostOrder(theRoot.Left); PostOrder(theRoot.Right); theRoot.DisplayNode();

} }

current = current.Right; if (current == null)
{

parent.Right = newNode;

public int FindMin() {

Node current = root;
while (!(current.Left == null))

current = current.Left; return current.Data;

}

public int FindMax() {

Node current = root;
while (!(current.Right == null))

current = current.Right; return current.Data;

}

public Node Find(int key) {

Node current = root;
while (current.Data != key) {

if (key < current.Data) current = current.Left;

else
current = current.Right; if (current == null)

return null; }

return current; }

static void Main() {

// }

}//end of public class BinarySearchTree }

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Question - modify the code below so that for a node, the value of every node...
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
  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

  • Lab 6 Help

    I need help with this lab assignment. I use C++, thank you. Derive a MinHeap class from the BST class of your Lab 4 code.Define/Override only the Search/Insert/Delete methods in the new class.Write a new main for this lab which:Will only be a test program for the MinHeap.Declares and creates a MinHeap object as necessary.Declares and creates the Dollar objects as necessary.Inserts the 20 Dollar objects specified in Lab 4 in the same order.Performs all 4 traversals after the inserting...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

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