Question

Algorithms and Data Structures

2. Let T be a binary search tree which implements a dictionary. Let v be a node of T, and T, be the subtree rooted at v Design a recursive algorithm CountLE(v, k) which, given an input node v and a key k, returns the number of entries in TU with key at most k

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

creating binary search tree and we can generate post order ,pre order,in order.In this program iam using random function for generation of numbers.

import java.util.Random;
class TreeNode
{
TreeNode leftNode;
int data;
TreeNode rightNode;
public TreeNode(int nodeData)
{
data=nodeData;
leftNode=rightNode=null;
}
public void insert(int insertValue)
{
if(insertValue<data)
{
if(leftNode==null)
leftNode=new TreeNode(insertValue);
else
leftNode.insert(insertValue);
}
else if(insertValue>data)
{
if(rightNode==null)
rightNode=new TreeNode(insertValue);
else
rightNode.insert(insertValue);
}
}
}
class Tree
{
private TreeNode root;
public Tree()
{
root=null;
}
public void insertNode(int insertValue)
{
if(root==null)
root=new TreeNode(insertValue);
else
root.insert(insertValue);
}
public void preorderTraversal()
{
preorderHelper(root);
}
private void preorderHelper(TreeNode node)
{
if(node==null)
return;
System.out.printf("%d",node.data);
preorderHelper(node.leftNode);
preorderHelper(node.rightNode);
}
public void inorderTraversal()
{
inorderHelper(root);
}
private void inorderHelper(TreeNode node)
{
if(node==null)
return;
inorderHelper(node.leftNode);
System.out.printf("%d",node.data);
inorderHelper(node.rightNode);
}
public void postorderTraversal()
{
postorderHelper(root);
}
private void postorderHelper(TreeNode node)
{
if(node==null)
return;
postorderHelper(node.leftNode);
postorderHelper(node.rightNode);
System.out.printf("%d",node.data);
}
}
public class TreeTraverse
{
public static void main(String args[])
{
Tree tree=new Tree();
int value;
Random randomNumber=new Random();
System.out.println("Inserting the following values:");
for(int i=1;i<=10;i++)
{
value=randomNumber.nextInt(100);
System.out.print(value+" ");
tree.insertNode(value);
}
System.out.println("\n\nPerorder traversal");
tree.preorderTraversal();
System.out.println("\n\nInorder traversal");
tree.inorderTraversal();
System.out.println("\n\npostorder traversal");
tree.postorderTraversal();
System.out.println();
}
}

// This method is used for counting the nodes in binary search tree

int count(struct tnode *p)

{

        if( p == NULL)

                return(0);

        else

                if( p->lchild == NULL && p->rchild == NULL)

                        return(1);

                else

                        return(1 + (count(p->lchild) + count(p->rchild)));

}

Add a comment
Know the answer?
Add Answer to:
Algorithms and Data Structures Let T be a binary search tree which implements a dictionary. Let...
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
  • Let T be a proper binary tree. Given a node v ∈ T, the imbalance of...

    Let T be a proper binary tree. Given a node v ∈ T, the imbalance of v, denoted imbalance(v), is defined as the difference, in absolute value, between the number of leaves of the left subtree of v and the number of leaves of the right subtree of v. (If v is a leaf, then imbalance(v) is defined to be 0.) Define imbalance(T) = maxv∈T imbalance(v). (a) Provide an upper bound to the imbalance of a proper binary tree with...

  • Data Structures and Algorithms What is the: a. maximum number of levels that a binary search...

    Data Structures and Algorithms What is the: a. maximum number of levels that a binary search tree with 100 nodes can have? b. minimum number of levels that a binary search tree with 100 nodes can have? c. maximum total number of nodes in a binary tree that has N levels? (Remember that the root is level 0.) d. maximum number of nodes in the Nth level of a binary tree? e. number of ancestors of a node in the...

  • java data structures and algorithms    binary trees part thank you very much Dagger Given a binary...

    java data structures and algorithms    binary trees part thank you very much Dagger Given a binary tree and a sum, the method has PathSum determines if the tree has a squareroot-to-leaf path such that adding up all the key values along the path equals the given sum. It uses an auxiliary method which takes the squareroot of the given tree to do its job as follows: public boolean hasPathSum(int sum){ return hasPathSum(root, sum); } Given the following tree where the...

  • 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...

  • Let T be a binary search tree. Show how to implement the following operation on T: countAlllnRange(Key lower, Key upper)...

    Let T be a binary search tree. Show how to implement the following operation on T: countAlllnRange(Key lower, Key upper): Compute and return the number of items in T with key k such that “lower ≤ k ≤ upper”. Assumption: The ADT Node offers a method “key” which returns the key value of the respective node. Give the running time of the operation in Big-O notation.

  • 13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...

    13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exist then the definition will display. You can also add words to the dictionary. For testing you...

  • In a binary tree, the balance ratio of node v, bal(v), is the number of nodes...

    In a binary tree, the balance ratio of node v, bal(v), is the number of nodes in the left subtree of node v divided by the sum of the number of nodes in the right and left subtrees of node v. bal(a leaf node) = ½. A tree T is said to be ε-balanced if, for all nodes v in T, ½ - ε < bal(v) < ½ + ε. Design an efficient recursive algorithm that determines whether a binary...

  • 3. [5 marks] Suppose T is a binary tree that stores an integer key value in...

    3. [5 marks] Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree. » the key T.key is the root node's integer key value . the left child T.left is T's left subtree, which is possibly an empty tree (or null) the right child T.right is T's right subtree, which is possibly an empty tree (or null) (a) Write an efficient algorithm FINDMAxPrODuCT(T) in pseudocode that returns...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • 1. What is the worst case time complexity of insertion into a binary search tree with...

    1. What is the worst case time complexity of insertion into a binary search tree with n elements? You should use the most accurate asymptotic notation for your answer. 2. A binary search tree is given in the following. Draw the resulting binary search tree (to the right of the given tree) after deleting the node with key value 8. 10 3. You have a sorted array B with n elements, where n is very large. Array C is obtained...

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
Active Questions
ADVERTISEMENT