Question

A collection of nodes is arranged as a binary search tree ordered on the field INFO which contains distinct positive integers

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

Algorithm for search operation'

  • The left subtree of a node is having  only nodes with keys less than the node’s .
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtree each must also be a binary search tree.
    There must be no duplicate nodes.
struct node* search(int info){
   struct node *currentnode = root;
   printf("Search elements: ");
        
   while(currentnode->info != info){
        
      if(currentnode != NULL) {
         printf("%d ",currentnode->info);
                        
         
         if(currentnode->info > info){
            currentnode = currentnode->leftChild;
         }  
         else {                
            currentnode = currentnode->rightChild;
         }
                        
         
         if(currentnode == NULL){
            return NULL;
         }


/* java implementation of Binary search Tree*/


import java.util.Scanner;



class Succ

{

Succ llink, rlink;

int info


public Succ()

{

llink = null;

rlink = null;

data = 0;

}

public Succ(int n)

{

llink = null;

rlink = null;

data = n;

}

public void placellink(Succ n)

{

llink = n;

}

public void placerlink(Succ n)

{

rlink = n;

}

public Succ getllink()

{

return llink;

}

public Succ getrlink()

{

return rlink;

}

/* Function to place data to node */

public void placeData(int d)

{

data = d;

}


public int getData()

{

return info

}

}



class Pred

{

private Succ root;


public Pred()

{

root = null;

}


public boolean isEmpty()

{

return root == null;

}


public void add(int data)

{

root = add(root, data);

}


private Succ add(Succ node, int data)

{

if (node == null)

node = new Succ(data);

else

{

if (node.getrlink() == null)

node.rlink = add(node.rlink, data);

else

node.llink = add(node.llink, data);

}

return node;

}


public int countNodes()

{

return countNodes(root);

}


private int countNodes(Succ r)

{

if (r == null)

return 0;

else

{

int l = 1;

l += countNodes(r.getllink());

l += countNodes(r.getrlink());

return l;

}

}

/* Function to search for an element */

public boolean search(int val)

{

return search(root, val);

}


private boolean search(Succ r, int val)

{

if (r.getData() == val)

return true;

if (r.getllink() != null)

if (search(r.getllink(), val))

return true;

if (r.getrlink() != null)

if (search(r.getrlink(), val))

return true;

return false;

}


public void inorderT()

{

inorderT(root);

}

private void inorderT(Succ r)

{

if (r != null)

{

inorderT(r.getllink());

System.out.print(r.getData() +" ");

inorderT(r.getrlink());

}

}


public void preorderT()

{

preorderT(root);

}

private void preorderT(Succ r)

{

if (r != null)

{

System.out.print(r.getData() +" ");

preorderT(r.getllink());

preorderT(r.getrlink());

}

}


public void postorderT()

{

postorderT(root);

}

private void postorderT(Succ r)

{

if (r != null)

{

postorderT(r.getllink());

postorderT(r.getrlink());

System.out.print(r.getData() +" ");

}

}

}

private Node remove(Succ current, int data) {

    if (current == null) {

        return null;

    }

    if (value == current.value) {

     

    }

    if (value < current.value) {

        current.llink = remove(current.llink, data);

        return current;

    }

    current.rightchild = remove(current.rightchild, data);

    return current;

}



/* Class BinaryTree */

public class BinaryTree

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

Pred Pred = new Pred();

System.out.println("Binary Tree Test\n");

char ch;

do

{

System.out.println("\nBinary Tree Operations\n");

System.out.println("1. add ");

System.out.println("2. search");

System.out.println("3. count nodes");

System.out.println("4. check empty");

System.out.println("5. remove");


int choice = scan.nextInt();

switch (choice)

{

case 1 :

System.out.println("Enter integer element to add");

Pred.add( scan.nextInt() );

break;

case 2 :

System.out.println("Enter integer element to search");

System.out.println("Search result : "+ Pred.search( scan.nextInt() ));

break;

case 3 :

System.out.println("Nodes = "+ Pred.countNodes());

break;

case 4 :

System.out.println("Empty status = "+ Pred.isEmpty());

break;

case 5 :

System.out.println("remove nodes= "+ Pred.remove());

break;

default :

System.out.println("Wrong Entry \n ");

break;

}

/* Display tree */

System.out.print("\nPost order : ");

Pred.postorderT();

System.out.print("\nPre order : ");

Pred.preorderT();

System.out.print("\nIn order : ");

Pred.inorderT();


System.out.println("\n\nDo you want to continue (Type y or n) \n");

ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');

}

}


      }                 
   }
   
   return current;
}
Add a comment
Know the answer?
Add Answer to:
A collection of nodes is arranged as a binary search tree ordered on the field INFO which contain...
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
  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent...

    QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent parent sibling QUESTION 2 In a tree, the ____ is a measure of the distance from a node to the root. count degree branch height level QUESTION 3 Which of the following is not a characteristic of a binary search tree? Each node has zero, one, or two successors. The preorder traversal processes the node first, then the left subtree, and then the right...

  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • a. How can I show that any node of a binary search tree of n nodes...

    a. How can I show that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations? b. using a, how can I show that any binary search tree can be balanced with at most O(n log n) rotations (“balanced” here means that the lengths of any two paths from root to leaf differ by at most 1)?

  • A binary search tree includes n nodes and has an height h. Check all that applies...

    A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity of TREE-MINIMUMX) TREE-MINIMUM () 1 while x. left NIL 2 3 return x x x.left O it is e (lg n) ■ it is 0(h). D it is e (1) ■ It is in place ■ it is Θ (n) A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

  • 2. A regular binary tree is a binary tree whose internal nodes all have two subtrees...

    2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • Suppose a binary search tree has multiple nodes containing the same key value, k. Let node...

    Suppose a binary search tree has multiple nodes containing the same key value, k. Let node p be the lowest common ancestor of such nodes. Then, the key of p must be also k. If this is true, provide an argument. Otherwise, provide a counterexample.

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