Question

In: Computer Science

Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of...

Binary Tree

  1. Develop algorithms for performing various operations of binary tree like insertion and deletion of elements, finding an element in the binary tree.
  2. Analyse time and space complexity of the designed algorithm
  3. Write C++ program to implement binary tree

Solutions

Expert Solution

Insertion

#include <iostream>

#include <queue>

using namespace std;

//Create a struct for key and left and right of the node

struct nod {

int key;

struct nod* lt, *rt;

};

//function to create a new nod of tree

struct nod* newnod(int key)

{

struct nod* tmp = new nod;

tmp->key = key;

tmp->lt = tmp->rt = NULL;

return tmp;

};

//Inorder traversal of a binary tree

void inorder(struct nod* tmp)

{

if (!tmp)

return;

inorder(tmp->lt);

cout << tmp->key << " ";

inorder(tmp->rt);

}

//function to insert element in binary tree

void insert(struct nod* tmp, int key)

{

queue<struct nod*> q;

q.push(tmp);

while (!q.empty()) {

struct nod* tmp = q.front();

q.pop();

if (!tmp->lt) {

tmp->lt = newnod(key);

break;

} else

q.push(tmp->lt);

if (!tmp->rt) {

tmp->rt = newnod(key);

break;

} else

q.push(tmp->rt);

}

}

int main()

{

struct nod* root = newnod(20);

root->lt = newnod(1);

root->lt->lt = newnod(6);

root->rt = newnod(5);

root->rt->lt = newnod(4);

root->rt->rt = newnod(3);

cout << "Inorder traversal before insertion:";

inorder(root);

int key = 13;

insert(root, key);

cout << endl;

cout << "Inorder traversal after insertion:";

inorder(root);

return 0;

}

Deletion

#include <bits/stdc++.h>
using namespace std;

/* A binary tree nod has k, pointer to lt
child and a pointer to rt child */
struct nod {
int k;
struct nod *lt, *rt;
};

//create a new nod of tree
struct nod* newnod(int k)
{
struct nod* tmp = new nod;
tmp->k = k;
tmp->lt = tmp->rt = NULL;
return tmp;
};

//inorder traversal
void in(struct nod* tmp)
{
if (!tmp)
return;
in(tmp->lt);
cout << tmp->k << " ";
in(tmp->rt);
}

// delete the given last node
void last(struct nod* root,
struct nod* d_nod)
{
queue<struct nod*> q;
q.push(root);

//level order traversal until last nod
struct nod* tmp;
while (!q.empty()) {
tmp = q.front();
q.pop();
if (tmp == d_nod) {
tmp = NULL;
delete (d_nod);
return;
}
if (tmp->rt) {
if (tmp->rt == d_nod) {
tmp->rt = NULL;
delete (d_nod);
return;
}
else
q.push(tmp->rt);
}

if (tmp->lt) {
if (tmp->lt == d_nod) {
tmp->lt = NULL;
delete (d_nod);
return;
}
else
q.push(tmp->lt);
}
}
}

// delete element in tree
nod* del(struct nod* root, int k)
{
if (root == NULL)
return NULL;

if (root->lt == NULL && root->rt == NULL) {
if (root->k == k)
return NULL;
else
return root;
}

queue<struct nod*> q;
q.push(root);

struct nod* tmp;
struct nod* k_nod = NULL;
while (!q.empty()) {
tmp = q.front();
q.pop();

if (tmp->k == k)
k_nod = tmp;

if (tmp->lt)
q.push(tmp->lt);

if (tmp->rt)
q.push(tmp->rt);
}

if (k_nod != NULL) {
int x = tmp->k;
last(root, tmp);
k_nod->k = x;
}
return root;
}


int main()
{
struct nod* root = newnod(11);
root->lt = newnod(15);
root->lt->lt = newnod(8);
root->lt->rt = newnod(16);
root->rt = newnod(19);
root->rt->lt = newnod(12);
root->rt->rt = newnod(1);

cout << "in traversal before del : ";
in(root);

int k = 19;
root = del(root, k);

cout << endl;
cout << "in traversal after del : ";
in(root);

return 0;
}

Search

#include <iostream>
using namespace std;

struct nod{
int data;
struct nod* lt, *rt;
nod(int data)
{
this->data = data;
lt= rt = NULL;
}
};

// Function to traverse the tree in preorder
// and check if the given nodxists in it
bool Exists(struct nod* nod, int key)
{
if (nod== NULL)
return false;

if (nod->data == key)
return true;


bool r1 = Exists(nod->lt, key);

bool r2 = Exists(nod->rt, key);

return r1 || r2;
}


int main()
{
struct nod* root = new nod(0);
root->lt= new nod(1);
root->lt->lt= new nod(3);
root->lt->lt->lt= new nod(7);
root->lt->rt = new nod(4);
root->lt->rt->lt= new nod(8);
root->lt->rt->rt = new nod(9);
root->rt = new nod(2);
root->rt->lt= new nod(5);
root->rt->rt = new nod(6);

int key = 4;

if (Exists(root, key))
cout << "YES";
else
cout << "NO";

return 0;
}

Analysis

Searching: for searching a element we have to traverse to all elements.Therfore searching in binary tree have a complexity of O(n)
Insertion: for insertion a element we have to traverse to all elements.Therfore insertion in binary tree have a complexity of O(n)
Deletion:for searching a elemet we have to traverse to all elements.Therfore searching in binary tree have a complexity of O(n)


Related Solutions

Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
1a) If I'm writing a program which involves many insertion and deletion operations, and memory is...
1a) If I'm writing a program which involves many insertion and deletion operations, and memory is not a concern, which of the following should I choose to hold my data? A: linked list B: fixed-size array C: dynamic array D: any of the above 1b) What about if I'm writing a program and I want the program to be flexible on the data with various sizes, and the program also needs to provide fast access to the data? Which of...
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
Compare and Contrast Hash Map to Binary Search Tree. Memory, Time Efficiency for Operations for Retrieving...
Compare and Contrast Hash Map to Binary Search Tree. Memory, Time Efficiency for Operations for Retrieving Singular Values, Maintaining relationships between data. Please be sure to use your own words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT