- Part 1Binary search tree
A node of a binary search tree (BST) of integers is defined as
typedef struct { int data; struct Node* left; struct Node* right;
} Node;
A binary search tree root is declared by
treePtr root = NULL;
where treePtr is defined as below.
typedef Node* treePtr;
Write a complete program BST.c to demonstrate basic operations on a binary search tree of integers. Your program should show the following options.
- Insert a new node (iteration)
- Insert a new node (recursion)
- Tree traversal
- Search a node (iteration)
- Search a node (recursion)
- Count number of nodes in tree
- Count number of leaves in tree
- Height of tree (root level = 0)
- Height of tree (root level = 1)
- Find the node with minimum key (iteration)
- Find the node with minimum key (recursion)
- Find the node with maximum key (iteration)
- Find the node with maximum key (recursion)
- Delete a node from BST (iteration)
- Delete a node from BST (recursion)
- Find the inorder successor (without using parent link)
- Breadth-first traversal (BFT)
- Exit
Select your choice (1-18):
The program allows a user to choose one of the above options by entering an integer from 1 to 18 to perform the corresponding action.
The marks are given as follows.
- Insert a new node (iteration). The insert() function allows the user to iteratively insert a new element into the BST.
- Insert a new node (recursion). The insertR() function allows the user to recursively add a new element into the BST.
- Tree traversal. This option allows the user to select the inorder, preorder, or postorder traversal by entering an integer 1, 2, or 3, respectively, to perform the corresponding action.
- Search a node (iteration). The search() function iteratively searches on the BST for the specified search key x, where x is input by the user. The search() function returns the node containing x if it is found; otherwise NULL is returned.
- Search a node (recursion). The searchR() function recursively searches on the BST for the specified search key x, where x is entered by the user. The searchR() function returns the node containing x if it is found; otherwise NULL is returned.
- Count number of nodes in tree. The countNodes() function returns the number of nodes in the BST.
- Count number of leaves in tree. The countLeaves() function returns the number of leaf nodes in the BST.
- Height of tree (root level = 0). The compHeight() function returns the height of the BST, where the height of a tree is the length of the longest simple path from the root to a leaf and the root is counted as level 0.
- Height of tree (root level = 1). The numLevels() function returns the height of the BST, where the height of a tree is the number of levels in the tree and the root is counted as level 1.
- Find the node with minimum key (iteration). The findMin() function iteratively searches on the BST for the node with the smallest value m of the data member. The findMin() function returns the node containing m.
- Find the node with minimum key (recursion). The findMinR() function recursively searches on the BST for the node with the smallest value m of the data member. The findMinR() function returns the node containing m.
- Find the node with maximum key (iteration). The findMax() function iteratively searches on the BST for the node with the largest value M of the data member. The findMax() function returns the node containing M.
- Find the node with maximum key (recursion). The findMaxR() function recursively searches on the BST for the node with the largest value M of the data member. The findMaxR() function returns the node containing M.
- Delete a node from BST (iteration). The Delete() function iteratively deletes an arbitrary node of the BST. Specifically, the program asks the user to enter the data member x of the node to be deleted. The Delete() function performs the deletion operation if x exists in the tree and returns 1; otherwise 0 is returned. The to-be-deleted node x is replaced with the leftmost node of the right subtree of x.
- Delete a node from BST (recursion). The DeleteR() function recursively deletes an arbitrary node of the BST. Specifically, the program asks the user to enter the data member x of the node to be deleted. The DeleteRR() function performs the deletion operation if x exists in the tree and returns the new tree (i.e., the tree with x has been deleted); otherwise displays the message Not found. The to-be-deleted node x is replaced with the leftmost node of the right subtree of x.
- Find the inorder successor (without using parent link). The inOrderSuccessor() function allows the user to find the inorder successor of a given node in the BST. The inorder successor of a given node is the node comes after the node in the inorder traversal of the BST. The rightmost node of the BST has no inorder successor.
- Breadth-first traversal (BFT). The BFT() function traverses the tree level by level, staring at the root. At each level, the nodes are traversed from left to right.
- Quit your program.
- Part 2 Hashing methods
Write the following complete C (or C++) programs.
- c to implement the linear probing method.
- c to implement the quadratic probing method.
- c to implement the double hashing method.
- c to implement the coalesced chaining method. 5. SC.c to implement the separate/direct chaining method.
- Each of the above programs should displays the following menu when it is executed.
- Insert a new key
- Search a given key
- Delete a given key
- Display hash table
- Quit
Select your option (1-5):
- Each of the above programs has the Insert(), Search(), Delete(), and Display() functions to perform the insertion, searching, deletion, and displaying operations, respectively. The Insert() function allows a user to insert a new key k into the hash table. It is supposed that the keys are distinct nonnegative integers. The Search() function allows a user to search the hash table for a given key k. The Delete() function allows a user to delete a given key k from the hash table. The Display() function shows the hash table contents on the screen.
- For the c program, the size of the hash table is M = 10. The hash function is defined as f(k)
= k % M, where the symbol % is the modulo operator and the key k is a nonnegative integer. The rehash function is ft(k) = (f(k) + t) % M, where the collision count t = 1, 2, . The Search() function returns the index i of k, 0 i M 1, if k is found; otherwise, returns M. Each node of the hash table is defined by typedef struct { int k; } Node;.
- For the c program, the size of the hash table is M = 10. The hash function is defined as f(k)
= k % M, where the symbol % is the modulo operator and the key k is a nonnegative integer. The rehash function is ft(k) = (f(k) + t2) % M, where the collision count t = 1, 2, . The Search() function returns the index i of k, 0 i M 1, if k is found; otherwise, returns M. Each node of the hash table is defined by typedef struct { int k; } Node;.
- For the c program, the size of the hash table is M = 11. The hash function is defined as f(k) = k % M, where the symbol % is the modulo operator and the key k is a nonnegative integer. The rehash function is ft(k) = (ft-1(k) + g(k)) % M, where the collision count t = 1, 2, ., the second hash function is g(k) = c (k % c), the constant c = 5, f0(k) = f(k). The Search() function returns the index i of k, 0 i M 1, if k is found; otherwise, returns M. Each node of the hash table is defined by typedef struct { int k; } Node;.
- For the c program, the size of the hash table is M = 10. The Search() function returns the index i of k, 0 i M 1, if k is found; otherwise, returns M. The Delete() function works as the following illustration. Each node of the hash table is defined by typedef struct { int k; int next; } Node;.
Suppose that the current state of the hash table is as follows.
Index | k | next | Index | k | next | Index | k | next | ||
0 | 10 | 9 | 0 | 10 | -1 | 0 | 30 | -1 | ||
1 | -1 | -1 | 1 | -1 | -1 | 1 | -1 | -1 | ||
2 | -1 | -1 | 2 | -1 | -1 | 2 | -1 | -1 | ||
3 | -1 | -1 | 3 | -1 | -1 | 3 | -1 | -1 | ||
4 | -1 | -1 | 4 | -1 | -1 | 4 | -1 | -1 | ||
5 | 15 | 8 | 5 | 15 | 8 | 5 | 15 | 8 | ||
6 | 26 | -1 | 6 | 26 | -1 | 6 | 26 | -1 | ||
7 | 35 | -1 | 7 | 35 | -1 | 7 | 35 | -1 | ||
8 | 25 | 7 | 8 | 25 | 7 | 8 | 25 | 7 | ||
9 | 30 | -1 | 9 | -1 | -1 | 9 | -1 | -1 |
Initial state If 30 is deleted. If 10 is deleted.
Index | k | next | Index | k | next | Index | k | next | ||
0 | 10 | 9 | 0 | 10 | 9 | 0 | 10 | 9 | ||
1 | -1 | -1 | 1 | -1 | -1 | 1 | -1 | -1 | ||
2 | -1 | -1 | 2 | -1 | -1 | 2 | -1 | -1 | ||
3 | -1 | -1 | 3 | -1 | -1 | 3 | -1 | -1 | ||
4 | -1 | -1 | 4 | -1 | -1 | 4 | -1 | -1 | ||
5 | 15 | 8 | 5 | 15 | 8 | 5 | 25 | 8 | ||
6 | 26 | -1 | 6 | 26 | -1 | 6 | 26 | -1 | ||
7 | -1 | -1 | 7 | -1 | -1 | 7 | -1 | -1 | ||
8 | 25 | -1 | 8 | 35 | -1 | 8 | 35 | -1 | ||
9 | 30 | -1 | 9 | 30 | -1 | 9 | 30 | -1 |
If 35 is deleted. If 25 is deleted. If 15 is deleted.
- For the c program, the size of the hash table is M = 10. The Search() function returns the pointer to the node containing k if k is found; otherwise, returns NULL. Each node of the
hash table is defined by typedef struct { int k; struct Node *next; } Node;.
Reviews
There are no reviews yet.