Posted by : Anonymous Tuesday, 27 November 2012

Assignment Statement: Marks: 20

A “Binary Search Tree” (BST) is a type of binary tree with the following property:

“Items in the left subtree are smaller than the root, and items in the right subtree are greater than the root”.



Consider the following Binary Search Tree (BST):




Keeping in mind the property of Binary Search Tree (BST), Write a program in C++ which stores the above tree (BST) incomputer memory and performs the following operations on the tree:


  1. Count total number of nodes in the tree
  2. Count number of leaf nodes in the tree
  3. Display value of each node using any of the traversal methods


Solution Guidelines:

Your Program should contain a BST class which should include the following functions:


  • insert_nodes( ) // to insert all nodes in the BST.
  • count_nodes( ) // to count total number of nodes in the tree
  • count_leaft_nodes( ) // to count number of leaf nodes in the tree
  • display( ) // to display nodes’ values on screen using any traversal methods (Preorder, Inorder, Postorder)


In main ( ) function, create an object of type “BST” and call above functions one by one against each operation.


Sample Output:


Note: In sample output, values of nodes are displayed using preorder traversal. 

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © virtual university of pakistan - Skyblue - Powered by Blogger - Designed by Johanes Djogan -