Starting Out With C++, Early Objects - With Access Package
8th Edition
ISBN: 9780133441840
Author: GADDIS
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 19, Problem 4PC
Program Plan Intro
Tree Height
Program Plan:
- Define a class BTreeNode to create nodes for binary search tree items.
- Include all the required header files.
- Initialize a value to the node, and set the left child of node to leftp and right child of node to rightp .
- Define a class to create a Binary Search Tree.
- Create a function int height that returns height of the tree.
- Create a function bool search to search a particular item in tree.
- Create function void insert to insert nodes into the tree.
- Create a function leafCount that counts no. of leaves present in a level.
- Create a function void inorder to sort items in inorder traversal.
- Declare the main function.
- Prompt the user to enter 5 numbers to be inserted into tree.
- Sort the items present in the tree in inorder traversal and return the height of the Binary Search Tree.
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
Binary search tree. Write a function named totalSum that takes as parameter the root of the binary search tree(with the following type) and returns the total sum of the numbers in the tree. struct tree{ int data;
struct tree *left, *right;
};
1.) Design a Binary Search Tree ADT by using following functions. Include the function definitions & run the program (Kindly include header files as well). Paste the output as a screenshot and write the code in your answer sheet.
insert(int val)find(int x)************************************************************************************ class btNode
{ public:
int info; btNode *lLink;
btNode *rLink;
btNode(int e, btNode *l = NULL, btNode *r = NULL)
{
info = e; lLink = l;
rLink = r;
}
btNode()
{
lLink = NULL;
rLink = NULL;
}
};
class binarySTADT
{ private:
btNode *root; int count = 0; public:
binarySTADT()
{
root = NULL;
}
void insert(int val); // function to insert a given value in the tree.…
F# Exercise, BST
write the following binary search tree functions in F# for a binary search tree of integers. Use the following type definition for a BST (copy this into your solution):
F# system functions (such as min) and the method in the List module such as List.map. are not allowed
// Tree definition for problem:
type BST =
| Empty
| TreeNode of int * BST * BST
insert value tree: Inserts the value into the tree and returns the resulting tree. The resulting tree does NOT need to be balanced. If the value already exists in the tree, return the tree without inserting the value.
search value tree: Returns true if the value is in the tree and false otherwise.
count func tree: The parameter func is a Boolean function that takes a single parameter and returns true or false. The function tests the value of each node with func and returns the number of nodes that evaluate to true.
evenCount tree: Returns the number of nodes that contain even integers. REQUIREMENT: This…
Chapter 19 Solutions
Starting Out With C++, Early Objects - With Access Package
Ch. 19.1 - Prob. 19.1CPCh. 19.1 - Prob. 19.2CPCh. 19.1 - Prob. 19.3CPCh. 19.1 - Prob. 19.4CPCh. 19.1 - Prob. 19.5CPCh. 19.1 - Prob. 19.6CPCh. 19.2 - Prob. 19.7CPCh. 19.2 - Prob. 19.8CPCh. 19.2 - Prob. 19.9CPCh. 19.2 - Prob. 19.10CP
Ch. 19.2 - Prob. 19.11CPCh. 19.2 - Prob. 19.12CPCh. 19 - Prob. 1RQECh. 19 - Prob. 2RQECh. 19 - Prob. 3RQECh. 19 - Prob. 4RQECh. 19 - Prob. 5RQECh. 19 - Prob. 6RQECh. 19 - Prob. 7RQECh. 19 - Prob. 8RQECh. 19 - Prob. 9RQECh. 19 - Prob. 10RQECh. 19 - Prob. 11RQECh. 19 - Prob. 12RQECh. 19 - Prob. 13RQECh. 19 - Prob. 14RQECh. 19 - Prob. 15RQECh. 19 - Prob. 16RQECh. 19 - Prob. 17RQECh. 19 - Prob. 18RQECh. 19 - Prob. 19RQECh. 19 - Prob. 20RQECh. 19 - Prob. 1PCCh. 19 - Prob. 2PCCh. 19 - Prob. 3PCCh. 19 - Prob. 4PCCh. 19 - Prob. 5PCCh. 19 - Prob. 6PCCh. 19 - Prob. 7PCCh. 19 - Prob. 8PCCh. 19 - Prob. 9PC
Knowledge Booster
Similar questions
- Given the following definition of a node and binary search tree, write the member functions described below. You may declare and implement more functions if you need but you need to fully code any function you call. Any function you call will calculate towards runtime of your function. Please ensure that the run time requirement is met to get full marks class BST{ struct Node{ int data_; Node* left_; Node* right_; Node(int dat){ data_=dat; left_=right_=nullptr; } }; Node* root_; ...}; Write the following member functions of BST: bool BST::removeSmallest(int v); Runtime requirement O(log n) (assuming tree is balanced) This function removes the smallest value from subtree with root value of varrow_forwardPart 1: I need to define a binary search tree data structure and need to implement the following functions: Insert Sorted BSTREE insert(BSTREE root, int num): root points to a node in a binary search tree; num is a number to be inserted in the tree rooted at “root”. This function returns the root of the modified tree. Print Elements void inorderTraversal(BSTREE root, FILE *fp): root points to a node in a binary search tree. This function does not return anything, but prints out, to the file specified, the nodes in the tree rooted at “root” by performing an inorder traversal. Part 2: Test the performance of the designed data structure using theoretical and experimental approaches as follows: Dataset 1-Dataset is sorted- Add code to insert the numbers 1...n in that order in an initially empty doubly linked list and a binary search tree. Run it on different values of n where : n = 20,000 n = 50,000 n = 100,000 Do an in-order traversal of the tree, printing out the contents…arrow_forwardAttached is a C program traveselter.c. This is a solution to the problem of constructing a binary tree from the inorder and preorder listing of the nodes. It has the recursive function void list_inorder(struct Node * root) which lists the nodes of the tree in-order. Rewrite this function so that it is iterative. Hint: Implement a stack and you may assume it can hold a maximum of 100 items. You have to decide what will be in an item of the stack, and design a structure for it. I suggest that one member of an item is a pointer to a tree node. #include <stdlib.h> #include <stdio.h> struct Node { int val; struct Node * left; struct Node * right; }; struct Node * create_node(char val); void destroy_node(struct Node * root); void destroy_tree(struct Node * root); void list_preorder(struct Node * root); struct Node * create_tree(char pre_order[], int pre_first, int pre_last, char in_order[], int in_first, int in_last); void list_inorder(struct Node *…arrow_forward
- C++ PROGRAMMINGTopic: Binary Search Trees Explain the c++ code below.: It doesn't have to be long, as long as you explain what the important parts of the code do. (The code is already implemented and correct, only the explanation needed) node* left(node* p) { return p->left; } node* right(node* p) { return p->right; } node* sibling(node* p){ if(p != root){ node* P = p->parent; if(left(P) != NULL && right(P) != NULL){ if(left(P) == p){ return right(P); } return left(P); } } return NULL; } node* addRoot(int e) { if(size != 0){ cout<<"Error"<<endl; return NULL; } root = create_node(e,NULL); size++; return root; } node* addLeft(node* p, int e) { if(p->left == NULL){ node* newLeft =…arrow_forwardPythin: A binary search tree, write a function that finds and returns the median value. Assume that the class member variable. [_size] contains the number of elements in the binary search tree. What is the time complexity of your function? def find_median(self):arrow_forwardProgramming questions:typedef struct node { int data; struct node *left, *right;}BT;The node structure of the binary tree (BT) is shown above. There is a binary tree T, please complete the function: int degreeone(BT *T) to compute how many degree 1 node in the BT. The T is the root pointer, and the function shoule return the total number of degree 1 node.arrow_forward
- Define the function (doubleBubbleLst lst). This function should resolve to a list of sublists, where each sublist holds a single element from lst and all sublists from lst are also bubbled, so that every list and sublist has no atoms. This is the deep recursion version of bubbleLst. For example: (doubleBubbleLst '(1 2 (3 4)) ) resolves to '((1) (2) (( (3) (4) ))).arrow_forwardC programming help get numbers from user using a struct that points(pointer) into a binary search tree and another pointer into a array to use merge sort on the array then prints out all the numbers and at what hight they are in the binary tree and also prints out the sorted arrayarrow_forwardC Programming language Binary Search Tree Empirical and Theoretical Results Part 1: we need to define a binary search tree data structure. Also, we need to implement the following functions: 1. Insert Sorted: BSTREE insert(BSTREE root, int num): root points to a node in a binary search tree; num is a number to be inserted in the tree rooted at "root". This function returns the root of the modified tree. 2. Print Elements: void inorder traversal(BSTREE root, FILE *fp): root points to a node in a binary search tree. This function does not return anything, but prints out, to the file specified, the nodes in the tree rooted at "root" by performing an inorder traversal. Part 2: Test the performance of the designed data structure using theoretical and experimental approaches as follows:1. Dataset 1-Dataset is sorted- Add code to insert the numbers 1...n in that order in an initially empty doubly linked list and a binary search tree. a. Run it on different values of n where : i.…arrow_forward
- A binary tree is balanced if every node in the tree is such that the heights of its left and right subtrees do not differ by more than one. Complete the pseudo-code method isBalanced which determines if a binary tree is balanced. The function returns a value of -1 if the tree is not balanced, otherwise it returns the height of the tree. Add comments to explain what your code segment is doing. The following definition of a node in the tree is used. class BinaryNode { int element; BinaryNode left; BinaryNode right; } Complete the method below: int isBalanced (BinaryNode t) { int leftCount=0; int rightCount=0; if (t == null) //empty tree, height is 0 return 0; if ((t.left== null) and (t.right== null) return 0; else { . . . . . . . . . } }arrow_forwardC programming I need help writing a code that uses a struct pointer into a binary tree and using the same pointer into an arrayarrow_forwardC programming I need a code that asks the user for how many times it'll run and run that many time and ask the user for a number and it puts that number into a binary search tree and an array using diffrent functionsarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education