(5/5)

Binary search trees using C++ templates

- Read about how to use templates in C++
- Complete the following C++ templates for binary search tree:

template class Node {

private:

T x;

Node *left; // left child Node *right; // right child Node *parent; // parent node

// any other augmented information

public:

};

//define suitable functions here

template class BST {

private:

Node *root; // root node

int n; // total number of nodes

public:

// define suitable constructor, destructors, etc. here. int search(T x); // search x in BST

int insert(T x); // insert x in BST int remove(T x); // delete x from BST

// return k-th smallest data in the tree T order_statistics(int k)

};

Here the operators <, >, ==, <=, >=, ! = are overloaded for type T .

Define instances of above class templates for different data types T , and test that they work correctly.

2 Performance of binary search trees on randomly ordered input

Without loss of generality, assume that the keys to be inserted in a BST are 1, 2, . . . , n. Let (σ(1), σ(2), . . . , σ(n)) be a random permutation of (1, 2, . . . , n) (i.e., each of the n! permutations are equally likely to be σ).

Suppose we insert keys σ(1), σ(2), . . . , σ(n) in an empty binary search tree in this order. Let Tσ be the resulting binary search tree.

Your objective is to experimentally estimate the average height of tree

Tσ. To be specific, let h(Tσ) be the height of tree Tσ. Then, average height

σ h(Tσ ) n!

Note. (i) You can try n = 128, 256, . . . , 65536 (successive powers of 2). For each n, you may generate K = 10000 permutations. Let height of binary search trees for these permutations are h1, h2, . . . , hK . Then, average height (of a random binary search tree) for n keys can be estimated by h1+h2+...+hK .

Plot this average height as a function of n. What do you observe?

Question: Can you conclude that if the elements to be inserted in a BST are given beforehand, a good strategy is to randomly permute them before constructing the BST?

(5/5)

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Get Free Quote!

358 Experts Online