Write a program that builds t BSTs by inserting N random keys into an initially empty tree, and then finds the tree height for N =100, 500 and 1000; and t =5, 10, 15. Find the average height of binary search trees for each pair of values of t and N. Decide what you will do with duplicates and explain what you did to handle duplicates.
package jan18; class Bst { private BstNode root; public void add(int value) { if (root == null) { root = new BstNode(value); } else { root.add(value); } } public int height() { if (root == null) { return 0; } else { return root.getHeight(); } } } class BstNode { private int data; private BstNode left; private BstNode right; public BstNode(int dataToSet) { data = dataToSet; left = null; right = null; } public void setData(int dataToSet) { data = dataToSet; } public void setLeft(BstNode linkToSet) { left = linkToSet; } public void setRight(BstNode linkToSet) { right = linkToSet; } public int getData() { return data; } public BstNode getLeft() { return left; } public BstNode getRight() { return right; } public void add(int value) { if (value == data) { return; } if (value > data) { if (right == null) { BstNode toAdd = new BstNode(value); right = toAdd; return; } else { right.add(value); return; } } else { if (left == null) { BstNode toAdd = new BstNode(value); left = toAdd; return; } else { left.add(value); return; } } } public int getHeight() { return getHeight(this); } // You may want to implement this private int getHeight(BstNode entry) { if (entry == null) { return 0; } return 1 + Math.max(getHeight(entry.left), getHeight(entry.right)); } } class BstDemo { public static void main(String[] args) { int N[] = {100, 500, 1000}; int t[] = {5, 10, 15}; for(int count: N) { for(int numTrees: t) { double totalHeight = 0; for(int i=0; i<numTrees; i++) { Bst tree = new Bst(); for(int k=0; k<count; k++) { tree.add((int) (Math.random() * (count * 10))); } totalHeight += tree.height(); } double avgHeight = totalHeight / numTrees; System.out.println("For N=" + count + " keys in " + numTrees + " trees, Average height=" + avgHeight); } } } }
We have ignored duplicates, since BST do not support duplicates.
Write a program that builds t BSTs by inserting N random keys into an initially empty tree, and then finds the tree height for N =100, 500 and 1000; and t =5, 10, 15. Find the average height of binary...
7. Suppose the same set of 31 values are inserted into two separate initially empty Binary Search Trees (BSTS). What is the maximum possible difference in height between the two trees? Answer 8. Which of the following is the most correct and relevant reason radix sort is not used for all sorting applications even though it can have a faster running time than comparison-based sorts? O Not all sorting applications Involve numerical data Not all sorting applications have bounded key...
a. The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y. Construct the tree and determine the output of the PREORDER traversal output. b. One main difference between a binary search tree (BST) and an AVL (Adelson-Velski and Landis) tree is that an AVL tree has a balance condition, that is, for every node in the AVL tree, the height of the left and right subtrees differ by at most 1....
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...