Starting Out With C++, Early Objects - With Access Package
Starting Out With C++, Early Objects - With Access Package
8th Edition
ISBN: 9780133441840
Author: GADDIS
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 19, Problem 1PC
Program Plan Intro

Simple Binary Search Tree Class

Program Plan:

  • Include the required header files.
  • Define the class BTreeNode.
    • Create constructor by passing the three parameters.
    • Declare the object for binary tree.
    • Declare class BST has friend.
  • Define the class BST.
    • Define the search() function.
      • Call search() function recursively and then return the result.
    • Declare insert() function prototype.
    • Define inorder() function.
      • Call inorder() function recursively and then return the result.
    • Declare the private search() function prototype.
    • Declare the private inorder() function prototype.
  • In the search() function,
    • Check whether the tree is empty. If yes, return false, there is no node in tree.
    • Check whether the tree is equal to x. If yes, return true, the search value is found
    • Check whether the tree is greater than x. If yes, call search() function by passing left node and result is returned.
    • Otherwise, call search() function by passing right node and result is returned.
  • In the insert() function,
    • Check whether the tree is empty. If yes, create an object for binary tree.
    • Loop executes until the tree is not empty. If yes,
      • Check whether x is less than or equal to tree. If yes, x goes to left node of binary tree.
      • Otherwise, x goes to right node of binary tree.
  • In the inorder() function,
    • Check whether the tree is empty. If yes, exit the statement.
    • Call inorder() function by passing left node.
    • List the inorder elements from tree.
    • Call inorder() function by passing right node.
  • Define the “main()” function.
    • Read the five inputs from user.
    • Call insert() function to insert all elements into binary tree.
    • Call inorder() function to inorder the elements present in binary tree.
    • Call search() function to search the specified values and then displays it.

Blurred answer
Students have asked these similar questions
Binary Search Tree Implementation(Java)In the binary search tree implementation, which is completely unrelated to the linked list implementation above, you will use an unbalanced binary search tree. Since duplicates are allowed, each node will have to store a singly-linked list of all the entries that are considered identical. Two values are identical if the comparator returns 0, even if the objects are unequal according to the equals method. A sketch of the private representation looks something like this: private Comparator<? super AnyType> cmp;private Node<AnyType> root = null; private void toString( Node<AnyType> t, StringBuffer sb ){ ... the recursive routine to be called by toString ... } private static class Node<AnyType>{private Node<AnyType> left;private Node<AnyType> right;private ListNode<AnyType> items; private static class ListNode<AnyType>{private AnyType data;private ListNode<AnyType> next; public ListNode( AnyType d,…
Attached 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 *…
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 =…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education