Saad Nasir cs151028 3-A
1. Create an insert function that will take nodes and add them up in the binary tree.
2. Create Inorder, PostOrder and PreOrder traversal functions for the binary tree.
3. Create a binary search function that tells whether a given value exists in the tree or not.
4. Create a delete function that searches the value in the tree, if it is present it deletes that value and return true else return false.
BONUS: Create a Breadth first traversal function for the binary.
*/
#include
#include
using namespace std;
struct node
{
int data; node *left; node *right;
public:
node(void) : left(NULL),right(NULL) { }
node(int data) : data(data), right(NULL),left(NULL) { }
};
class binaryTree
{
public: …show more content…
void PostOrder_print(node *p); void Search(int value); void Delete(int value);
void BreadthFirstTraversal(node* root);
};
binaryTree::binaryTree()
{
root=NULL;
}
binaryTree::~binaryTree()
{
}
void binaryTree::insert(int value,node *newNode) { int a=0;
if ( root == NULL ) { root = newNode; root->left=NULL; root->right=NULL; a++; } node *p=root; while(true) { if(a>=1) { break; } if ( value < p->data ) {
if(p->left==NULL) { p->left=newNode; break; } else if(p->left!=NULL) { p=p->left; }
} else if(value > p->data) { if(p->right==NULL) { p->right=newNode;