SlideShare a Scribd company logo
Given a newly created Binary Search Tree with the following numerical key input sequence
(from left to right): 9, 4, 12, 7, 5, 2, 20, 14, 11, 13, 19, 16, 17, 42, 24
a.) We are asked to add a function Position lowestKey(const Position& v) const to the class
SearchTree.
Function prototype: Position lowestKey(const Position& v);
input argument: position of the starting node in the binary search tree to calculate from. For the
whole tree this would be the position for the root of the tree.
return value: position of the node having the lowest key value.
i. Describe strategy with reasonings and examples to convince that it will work.
ii. Implement the function using C++ and show the source code listing.
b) We are also asked to add a function Position highestKey(const Position& v) const to the class
SearchTree.
Function prototype: Position highestKey(const Position& v) const;
input argument: position of the node in the binary search tree to calculate from. For the whole
tree this would be the position for the root of the tree.
return value: position of the node having the highest key value.
i. Describe strategy with reasonings and examples to convince that it will work.
ii. Implement the function using C++ and show the source code listing.
Solution
#include
#include
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(data));
else
{
/* 2. Otherwise, recur down the tree */
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
/* return the (unchanged) node pointer */
return node;
}
}
/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node) {
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}
/* Driver program to test sameTree function*/
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
printf(" Minimum value in BST is %d", minValue(root));
getchar();
return 0;
}
#include
#include
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(data));
else
{
/* 2. Otherwise, recur down the tree */
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
/* return the (unchanged) node pointer */
return node;
}
}
/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node) {
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}
/* Driver program to test sameTree function*/
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
printf(" Minimum value in BST is %d", minValue(root));
getchar();
return 0;
}

More Related Content

Similar to Given a newly created Binary Search Tree with the following numerica.pdf (20)

PDF
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
PPTX
Linked List.pptx
PoonamPatil120
 
DOCX
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
festockton
 
PPT
Linkedlist
Masud Parvaze
 
PDF
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
anton291
 
DOCX
DS UNIT5_BINARY TREES.docx
VeerannaKotagi1
 
DOCX
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
festockton
 
PPTX
C Assignment Help
Programming Homework Help
 
PDF
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
PPT
Lecture 7-BinarySearchTrees.ppt
DrBashirMSaad
 
PDF
TutorialII_Updated____niceupdateprogram.pdf
BappyAgarwal
 
PDF
Linked list
A. S. M. Shafi
 
PPT
linkedlistwith animations.ppt
MuhammadShafi89
 
PPTX
DS group binary tree all information M.pptx
AjajKhan23
 
PDF
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
PDF
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
fathimalinks
 
PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
adityastores21
 
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
PPT
dynamicList.ppt
ssuser0be977
 
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
debishakespeare
 
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
Linked List.pptx
PoonamPatil120
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
festockton
 
Linkedlist
Masud Parvaze
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
anton291
 
DS UNIT5_BINARY TREES.docx
VeerannaKotagi1
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
festockton
 
C Assignment Help
Programming Homework Help
 
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Lecture 7-BinarySearchTrees.ppt
DrBashirMSaad
 
TutorialII_Updated____niceupdateprogram.pdf
BappyAgarwal
 
Linked list
A. S. M. Shafi
 
linkedlistwith animations.ppt
MuhammadShafi89
 
DS group binary tree all information M.pptx
AjajKhan23
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
fathimalinks
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
adityastores21
 
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
dynamicList.ppt
ssuser0be977
 
Required to augment the authors Binary Search Tree (BST) code to .docx
debishakespeare
 

More from hadpadrrajeshh (20)

PDF
Gather information on the following two disasters Space shuttle Ch.pdf
hadpadrrajeshh
 
PDF
Determine the Laplace transform, F(s), of the following time signal..pdf
hadpadrrajeshh
 
PDF
Describe the difference between a monitor and a semaphoreSolutio.pdf
hadpadrrajeshh
 
PDF
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
hadpadrrajeshh
 
PDF
A speedometer is a measuring instrument. What are the independent and.pdf
hadpadrrajeshh
 
PDF
A recent study by the EPA has determined that the amount of contamin.pdf
hadpadrrajeshh
 
PDF
choice one correct answerRocks containing iron, calcium, magnesium.pdf
hadpadrrajeshh
 
PDF
1.write down the RTL for the SUBT instructions. 2.Draw a flowchart.pdf
hadpadrrajeshh
 
PDF
Write a Java application that asks for an integer and returns its fac.pdf
hadpadrrajeshh
 
PDF
Where should seaweeds and kelps be classified Give characteristics .pdf
hadpadrrajeshh
 
PDF
What is the name of the connective tissue that divides the se.pdf
hadpadrrajeshh
 
PDF
What is the current ratio of Chester Use the results of the Balance.pdf
hadpadrrajeshh
 
PDF
What is glass-transition temperature Give a material example. How ma.pdf
hadpadrrajeshh
 
PDF
What are some contributions to the development of industrializat.pdf
hadpadrrajeshh
 
PDF
The “creative revolution” that handed more authority to agency art d.pdf
hadpadrrajeshh
 
PDF
The three main coefficients (not properties or dimensionless numbers).pdf
hadpadrrajeshh
 
PDF
The project will study the coordination of multiple threads using se.pdf
hadpadrrajeshh
 
PDF
The positive selection process for T lymphocytes that occurs In the t.pdf
hadpadrrajeshh
 
PDF
The local public health agency has received reports of an outbreak o.pdf
hadpadrrajeshh
 
PDF
how are musical sounds produced Explain your reasoning.Solution.pdf
hadpadrrajeshh
 
Gather information on the following two disasters Space shuttle Ch.pdf
hadpadrrajeshh
 
Determine the Laplace transform, F(s), of the following time signal..pdf
hadpadrrajeshh
 
Describe the difference between a monitor and a semaphoreSolutio.pdf
hadpadrrajeshh
 
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
hadpadrrajeshh
 
A speedometer is a measuring instrument. What are the independent and.pdf
hadpadrrajeshh
 
A recent study by the EPA has determined that the amount of contamin.pdf
hadpadrrajeshh
 
choice one correct answerRocks containing iron, calcium, magnesium.pdf
hadpadrrajeshh
 
1.write down the RTL for the SUBT instructions. 2.Draw a flowchart.pdf
hadpadrrajeshh
 
Write a Java application that asks for an integer and returns its fac.pdf
hadpadrrajeshh
 
Where should seaweeds and kelps be classified Give characteristics .pdf
hadpadrrajeshh
 
What is the name of the connective tissue that divides the se.pdf
hadpadrrajeshh
 
What is the current ratio of Chester Use the results of the Balance.pdf
hadpadrrajeshh
 
What is glass-transition temperature Give a material example. How ma.pdf
hadpadrrajeshh
 
What are some contributions to the development of industrializat.pdf
hadpadrrajeshh
 
The “creative revolution” that handed more authority to agency art d.pdf
hadpadrrajeshh
 
The three main coefficients (not properties or dimensionless numbers).pdf
hadpadrrajeshh
 
The project will study the coordination of multiple threads using se.pdf
hadpadrrajeshh
 
The positive selection process for T lymphocytes that occurs In the t.pdf
hadpadrrajeshh
 
The local public health agency has received reports of an outbreak o.pdf
hadpadrrajeshh
 
how are musical sounds produced Explain your reasoning.Solution.pdf
hadpadrrajeshh
 
Ad

Recently uploaded (20)

PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Dimensions of Societal Planning in Commonism
StefanMz
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Ad

Given a newly created Binary Search Tree with the following numerica.pdf

  • 1. Given a newly created Binary Search Tree with the following numerical key input sequence (from left to right): 9, 4, 12, 7, 5, 2, 20, 14, 11, 13, 19, 16, 17, 42, 24 a.) We are asked to add a function Position lowestKey(const Position& v) const to the class SearchTree. Function prototype: Position lowestKey(const Position& v); input argument: position of the starting node in the binary search tree to calculate from. For the whole tree this would be the position for the root of the tree. return value: position of the node having the lowest key value. i. Describe strategy with reasonings and examples to convince that it will work. ii. Implement the function using C++ and show the source code listing. b) We are also asked to add a function Position highestKey(const Position& v) const to the class SearchTree. Function prototype: Position highestKey(const Position& v) const; input argument: position of the node in the binary search tree to calculate from. For the whole tree this would be the position for the root of the tree. return value: position of the node having the highest key value. i. Describe strategy with reasonings and examples to convince that it will work. ii. Implement the function using C++ and show the source code listing. Solution #include #include /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { int data; struct node* left; struct node* right; }; /* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data)
  • 2. { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } /* Give a binary search tree and a number, inserts a new node with the given number in the correct place in the tree. Returns the new root pointer which the caller should then use (the standard trick to avoid using reference parameters). */ struct node* insert(struct node* node, int data) { /* 1. If the tree is empty, return a new, single node */ if (node == NULL) return(newNode(data)); else { /* 2. Otherwise, recur down the tree */ if (data <= node->data) node->left = insert(node->left, data); else node->right = insert(node->right, data); /* return the (unchanged) node pointer */ return node; } } /* Given a non-empty binary search tree, return the minimum data value found in that tree. Note that the entire tree does not need
  • 3. to be searched. */ int minValue(struct node* node) { struct node* current = node; /* loop down to find the leftmost leaf */ while (current->left != NULL) { current = current->left; } return(current->data); } /* Driver program to test sameTree function*/ int main() { struct node* root = NULL; root = insert(root, 4); insert(root, 2); insert(root, 1); insert(root, 3); insert(root, 6); insert(root, 5); printf(" Minimum value in BST is %d", minValue(root)); getchar(); return 0; } #include #include /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { int data; struct node* left; struct node* right; }; /* Helper function that allocates a new node with the given data and NULL left and right pointers. */
  • 4. struct node* newNode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } /* Give a binary search tree and a number, inserts a new node with the given number in the correct place in the tree. Returns the new root pointer which the caller should then use (the standard trick to avoid using reference parameters). */ struct node* insert(struct node* node, int data) { /* 1. If the tree is empty, return a new, single node */ if (node == NULL) return(newNode(data)); else { /* 2. Otherwise, recur down the tree */ if (data <= node->data) node->left = insert(node->left, data); else node->right = insert(node->right, data); /* return the (unchanged) node pointer */ return node; } } /* Given a non-empty binary search tree, return the minimum data value found in that
  • 5. tree. Note that the entire tree does not need to be searched. */ int minValue(struct node* node) { struct node* current = node; /* loop down to find the leftmost leaf */ while (current->left != NULL) { current = current->left; } return(current->data); } /* Driver program to test sameTree function*/ int main() { struct node* root = NULL; root = insert(root, 4); insert(root, 2); insert(root, 1); insert(root, 3); insert(root, 6); insert(root, 5); printf(" Minimum value in BST is %d", minValue(root)); getchar(); return 0; }