Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from saurabhh-svg/main
Binary Search Tree Template
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include<iostream> | ||
#include "BST.h" | ||
#include <queue> | ||
using namespace std; | ||
|
||
|
||
BinaryTreeNode<int> *takeInputLevelWise() | ||
{ | ||
int rootData; | ||
cout << "Enter Root Data" << endl; | ||
cin >> rootData; | ||
if (rootData == -1) | ||
return NULL; | ||
BinaryTreeNode<int> *root = new BinaryTreeNode<int>(rootData); | ||
queue<BinaryTreeNode<int> *> pendingNodes; | ||
pendingNodes.push(root); | ||
|
||
while (pendingNodes.size() != 0) | ||
{ | ||
BinaryTreeNode<int> *front = pendingNodes.front(); | ||
pendingNodes.pop(); | ||
cout << "Enter Left child of " << front->data << endl; | ||
int leftChildData; | ||
cin >> leftChildData; | ||
if (leftChildData != -1) | ||
{ | ||
BinaryTreeNode<int> *child = new BinaryTreeNode<int>(leftChildData); | ||
front->left = child; | ||
pendingNodes.push(child); | ||
} | ||
cout << "Enter Right child of " << front->data << endl; | ||
int rightChildData; | ||
cin >> rightChildData; | ||
if (rightChildData != -1) | ||
{ | ||
BinaryTreeNode<int> *child = new BinaryTreeNode<int>(rightChildData); | ||
front->right = child; | ||
pendingNodes.push(child); | ||
} | ||
} | ||
return root; | ||
} | ||
|
||
|
||
|
||
int main() | ||
{ | ||
BST b; | ||
b.insert(10); | ||
b.insert(5); | ||
b.insert(20); | ||
b.insert(7); | ||
b.insert(13); | ||
b.insert(3); | ||
b.insert(15); | ||
// b.printTree(); | ||
b.deleteData(10); | ||
b.printTree(); | ||
} | ||
|
||
// for input copy paste -> | ||
// 1 2 3 4 5 6 7 -1 -1 -1 -1 8 9 -1 -1 -1 -1 -1 -1 |