Coder Social home page Coder Social logo

abdullahsattaar / binary-search-trees Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 4 KB

C++ code to implement the Class of Binary Search Trees. Recursive insert operation, recursive inorder traversal, and some other recursive operations on BST

C++ 100.00%
binary-search-tree cpp data-structures tree-node

binary-search-trees's Introduction

Binary-Search-Trees

C++ code to implement the Class of Binary Search Trees. Recursive insert operation, recursive inorder traversal, and some other recursive operations on BST.

Implement the following Tree Node:

template <typename k, typename v> struct TNode { k key; v value; TNode<k, v> *leftChild; TNode<k, v> *rightChild; }

Now implement a binary search tree class “BST” which contains root of type TNode as data member. You have to implement the following member functions for your binary search tree:

a. A default Constructor which sets the root to nullptr.

b. A recursive “insertRec” function which is passed as parameter a key and a corresponding value. It then uses recursion to insert the <key, value> pair while considering the insertion rules. If the key already exists in the BST, it simply replaces the value. void insertRec(k const key, v const value)

c. A function “insert” which is passed as parameter a key and a corresponding value. It then iteratively inserts the <key, value> pair while considering the insertion rules. If the key already exists in the BST, simply replace the value. void insert(k const key, v const value)

d. A function “search” which is passed as parameter a key. The function then uses recursion to return pointer to the corresponding value. If the key does not exist, the function returns null. v* search(k key)

e. A function “search” which is passed as parameter a key. The function then uses iteration to return pointer to the corresponding value. If the key does not exist, the function returns null. v* search(k key)

f. A function “inorderPrintkeysRec” which prints the keys using recursive inorder traversal. void inorderPrintKeysRec() const

g. A function “inorderPrintkeys” which prints the keys using iterative inorder traversal. void inorderPrintKeys() const

h. A function “NoofNodes” which uses recursion to return the count of total nodes in BST. int NoofNodes() const

i. A function “NoofNodes” which uses iteration to return the count of total nodes in BST. int NoofNodes() const

j. A function “heightofTree” which uses recursion to return the height of BST. int heightofBST() const

k. A function “heightofTree” which uses iteration to return the height of BST. int heightofBST() const

i. destructor

int main() { BST<int, int> tree; //the key and value both are of type int

tree.insert(500, 500);
tree.insertRec(1000, 1000);
tree.insert(1, 1);
tree.insert(600, 600);
tree.insertRec(700, 700);
tree.insert(10, 10);
tree.insert(30, 30);
tree.insertRec(9000, 9000);
tree.insert(50000, 50000);
tree.insertRec(20, 20);


cout << "Printing keys using iterative inorder traversal: ";
tree.inorderPrintKeys();

cout << endl << endl << "Printing keys using recursive inorder traversal: ";
tree.inorderPrintKeysRec();

cout << endl << endl<< "Tree Length: " << tree.length() << endl << endl;

int *val = tree.search(1);

if (val != nullptr)
{
	cout << "1 found" << endl;
}

val = tree.search(123);

if (val == nullptr)
{
	cout << "123 not found" << endl;
}

cout <<endl<< 
cout<<tree. NoofNodes();  //display number of nodes
cout<<tree. heightofBST(); //display height of BST

system("pause");

}

binary-search-trees's People

Contributors

abdullahsattaar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.