C++ implementation
Return the number of nodes in a binary search tree that are
greater than the amount that the user enters.
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
vector<Node*> child;
};
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
return temp;
}
int nodesGreater(Node* root, int search)
{
if (root == NULL)
return 0;
int count = 0;
if(root->data > search)
count++;
int numNodes = root->child.size();
for (int i = 0; i < numNodes; i++)
{
Node* child = root->child[i];
count += nodesGreater(child, search);
}
return count;
}
int main()
{
Node* root = newNode(5);
(root->child).push_back(newNode(6));
(root->child).push_back(newNode(3));
(root->child).push_back(newNode(4));
(root->child[0]->child).push_back(newNode(22));
(root->child[1]->child).push_back(newNode(17));
(root->child[1]->child).push_back(newNode(9));
(root->child[2]->child).push_back(newNode(8));
cout << "\nDemo Binary search tree has been constructed." << endl;
int search;
cout << "Please enter an integer to search: ";
cin >> search;
int nNodesGreater = nodesGreater(root, search);
if(nNodesGreater == 0)
cout << "Sorry, there are no nodes greater than " << search << endl;
else
cout << "Number of nodes greater than " << search << " are = " << nNodesGreater << endl;
return 0;
}
********************************************************** SCREENSHOT ******************************************************
C++ implementation Return the number of nodes in a binary search tree that are greater than...
A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10, 6, 4, 8, 18, 15, 21 Please type...
The linked implementation is used in the coding of a Binary Search Tree structure. Calculate the structure's density assuming that it contains 200 nodes and: a. Each node contains 10 bytes of information. b. Each node contains 300 bytes of information. Then, repeat the above exercise for the array implementation of the Binary Search Tree, assuming it is: a. Left balanced. b. Skewed to the right.
C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)
Show that the tree height of a height-balanced binary search tree with n nodes is O(log n). (Hint: Let T(h) denote the fewest number of nodes that a height-balanced binary search tree of height h can have. Express T(h) in terms of T(h-1) and T(h-2). Then, find a lower bound of T(h) in terms of T(h-2). Finally, express the lower bound of T(h) in terms of h.)
(C++ Psuedocode) A pair of nodes x, y in a (supposed) binary search tree violate the BST property if x is an ancestor of y, and the corresponding values are “out of order”. Given a BST, find the number of pairs that violate the BST property.
A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity of TREE-MINIMUMX) TREE-MINIMUM () 1 while x. left NIL 2 3 return x x x.left O it is e (lg n) ■ it is 0(h). D it is e (1) ■ It is in place ■ it is Θ (n) A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity...
Binary Search tree Implementation of a BST class that include the following operations: - Insertion, Search, Deletion - Traversals: Inorder, Preorder, Postorder using c++
C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...
Data Structures and Algorithms What is the: a. maximum number of levels that a binary search tree with 100 nodes can have? b. minimum number of levels that a binary search tree with 100 nodes can have? c. maximum total number of nodes in a binary tree that has N levels? (Remember that the root is level 0.) d. maximum number of nodes in the Nth level of a binary tree? e. number of ancestors of a node in the...
Design a recursive algorithm that determines whether the number of leaf nodes of a Binary Search Tree (BST) is even or odd. An empty tree has an even number of leaves. A tree consisting of just a root has an odd number of leaves. Your function should return true for an even number of leaves, and false for an odd number of leaves. Analyze the execution time of your algorithm. Giving only the execution time without explanation will not be...