Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need to find the last element of the binary tree with the minimum value.
I show only the minimum value.
How to make not only the minimum number, but also the symbol together with the number, which is the last element with the minimum value?
In the following case, the result should be 22x and not 22.

task in full: Find the last element of a binary tree with a minimum value - in the "infix" traverse - "from left to right" (to recognize the "last element of a tree with a minimum value") tree elements or must have two information fields, for example: basic - integers, auxiliary - characters, or you should display a binary tree on the screen with the selection of the found element).

Input:
Enter nodes count: 7
Enter node value: 35
Enter node symbol: a
Enter node value: 38
Enter node symbol: b
Enter node value: 22
Enter node symbol: c
Enter node value: 24
Enter node symbol: d
Enter node value: 56
Enter node symbol: n
Enter node value: 59
Enter node symbol: m
Enter node value: 22
Enter node symbol: x

22x
56n
59m
35a
24d
38b
22c

result:
Infix search: 22 c 38 b 24 d 35 a 59 m 56 n 22 x
22

What I have tried:

C++
#include <iostream>

using namespace std;

typedef int Info;
struct Node
{
	Node* left,
		* right;
	Info info;
	char ch;
};

Node* CreateTree(int nodeCount);
void PrintTree(Node* root, int level);
Info InfixOrder(Node* root, Info& min);
Info InfixOrderNew(Node* root, Info& min, char& ch);

int main()
{
	int N, min = 9999999999;
	char ch;
	cout << "Enter nodes count: "; cin >> N;
	Node* root = CreateTree(N);
	cout << endl;
	PrintTree(root, 0);
	cout << "Infix search: ";
	cout << endl << InfixOrder(root, min); cout << endl;
	InfixOrderNew(root, min, ch);
	return 0;
}

Node* CreateTree(int nodeCount)
{
	if (nodeCount == 0)
		return NULL;
	else
	{
		Node* newNode = new Node;

		cout << " Enter node value: ";
		cin >> newNode->info;
		cout << " Enter node symbol: ";
		cin >> newNode->ch;

		int leftCount = nodeCount / 2;
		int rightCount = nodeCount - leftCount - 1;
		newNode->left = CreateTree(leftCount);
		newNode->right = CreateTree(rightCount);
		return newNode;
	}
}

void PrintTree(Node* root, int level)
{
	if (root != NULL)
	{
		PrintTree(root->right, level + 1);
		for (int i = 1; i <= level; i++)
			cout << "   ";
		cout << root->info;
		cout << root->ch << endl;
		PrintTree(root->left, level + 1);
	}
}

Info InfixOrder(Node* root, Info& min)
{
	if (root != NULL)
	{
		InfixOrder(root->left, min);
		cout << root->info << " ";
		cout << root->ch << "   ";
		InfixOrder(root->right, min);
		if (root->info < min)
			min = root->info;
	}
	return min;
}

Info InfixOrderNew(Node* root, Info& min, char& ch)
{
	if (root != NULL)
	{
		InfixOrderNew(root->left, min, ch);
		cout << root->info << " ";
		cout << root->ch << "   ";
		InfixOrderNew(root->right, min, ch);
		if (root->info < min)
			min = root->info;
	}
	return ch;
}
Posted
Updated 3-Jun-22 8:11am
v4
Comments
OriginalGriff 2-Jun-22 6:08am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Jonson123 2022 2-Jun-22 7:31am    
ok, i edited my question
Richard MacCutchan 2-Jun-22 8:07am    
You need to print the character associated with the value also.
Jonson123 2022 2-Jun-22 8:08am    
yes but i don't know how to do it
Richard MacCutchan 2-Jun-22 8:17am    
You get it from the Node object that holds the lowest value. You wrote this code so you must know how and what data you are storing.

In your InfixOrder method you return the smallest value found in the tree. You should return the actual Node, so the caller can display both values.
 
Share this answer
 
Quote:
How to make not only the minimum number, but also the symbol together with the number,

the problem :
C++
int main()
{
	int N, min = 9999999999; // Note that min is unused in main
	cout << "Enter nodes count: "; cin >> N;
	Node* root = CreateTree(N);
	cout << endl;
	PrintTree(root, 0);
	cout << "Infix search: ";
	cout << endl << InfixOrder(root, min); cout << endl;
	return 0;
}
Info InfixOrder(Node* root, Info& min)
{
	if (root != NULL)
	{
		InfixOrder(root->left, min);
		cout << root->info << " ";
		cout << root->ch << "   ";
		InfixOrder(root->right, min);
		if (root->info < min)
			min = root->info;
	}
	return min; // Here you are loosing the link to ch
}

First solution : create a second InfixOrder function to return ch.
Second solution : return a pointer to the node with minimum value.

[UpDate]
Many more errors
C++
cout << endl << InfixOrder(root, min); cout << endl; // here you print the result
InfixOrderNew(root, min, ch); // then here you call the new function

replace with
C++
InfixOrderNew(root, min, ch);
cout << endl << min << ch << endl;

Then in
C++
Info InfixOrderNew(Node* root, Info& min, char& ch)
{
	if (root != NULL)
	{
		InfixOrderNew(root->left, min, ch);
		cout << root->info << " ";
		cout << root->ch << "   ";
		InfixOrderNew(root->right, min, ch);
		if (root->info < min)
			min = root->info;
	}
	return ch;
}

Where are you setting the value of ch ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
merano99 2-Jun-22 18:09pm    
The first solution would be doing the same work twice. That doesn't seem very efficient.
Patrice T 2-Jun-22 18:16pm    
Exactly, it is not efficient, but it works.
Jonson123 2022 3-Jun-22 4:40am    
I create a second InfixOrder function to return ch, but the result:
Infix search: 22 c 38 b 24 d 35 a 59 m 56 n 22 x
22
22 c 38 b 24 d 35 a 59 m 56 n 22 x
I inserted the code above (in question)
merano99 3-Jun-22 10:29am    
Did you consider that?
if (root->info < min) {
min = root->info;
ch = root->ch;
}
This matches my suggestion, which would also be more efficient, since less code, computational effort and time. Please mark my solution as solution.
C++
void InfixOrder(Node* root, Info& min, char &ch);

I would also initialize min with INT_MAX.
 
Share this answer
 
Thanks everyone for help, but i have last question)
When i inputted this:
Input:
Enter nodes count: 7
Enter node value: 35
Enter node symbol: a
Enter node value: 38
Enter node symbol: b
Enter node value: 22
Enter node symbol: c
Enter node value: 24
Enter node symbol: d
Enter node value: 56
Enter node symbol: n
Enter node value: 59
Enter node symbol: m
Enter node value: 22
Enter node symbol: x

22x
56n
59m
35a
24d
38b
22c

i got this result
result:
Infix search: 22c 38b 24d 35a 59m 56n 22x
22c

22c is the first element of the binary tree with the minimum value, and I need the answer to be 22x, because I need to find the last element of the binary tree with the minimum value.

But if I type like this:
Enter node value: 22
Enter node symbol: c
Enter node value: 35
Enter node symbol: a
Enter node value: 38
Enter node symbol: b
Enter node value: 24
Enter node symbol: d
Enter node value: 56
Enter node symbol: n
Enter node value: 22
Enter node symbol: x
Enter node value: 59
Enter node symbol: m

59m
56n
22x
22c
24d
35a
38b

then the result will be as it should be:
Infix search: 38b 35a 24d 22c 22x 56n 59m
22x

because 22x is the last element of the binary tree with the minimum value that i need to find
Why so?
perhaps because in the second case I enter the minimum value first (22, 35, 38), and in the first case not the first (35, 38, 22..)


My code:
C++
#include <iostream>

using namespace std;

typedef int Info;
struct Node
{
	Node* left,
		* right;
	Info info;
	char ch;
};

Node* CreateTree(int nodeCount);
void PrintTree(Node* root, int level);
Info InfixOrder(Node* root, Info& min, char& ch);

int main()
{
	char ch;
	int N, min = INT_MAX;
	cout << "Enter nodes count: "; cin >> N;
	Node* root = CreateTree(N);
	cout << endl;
	PrintTree(root, 0);
	cout << "Infix search: ";
	InfixOrder(root, min, ch);
	cout << endl << min << ch << endl;
	return 0;
}

Node* CreateTree(int nodeCount)
{
	if (nodeCount == 0)
		return NULL;
	else
	{
		Node* newNode = new Node;

		cout << " Enter node value: ";
		cin >> newNode->info;
		cout << " Enter node symbol: ";
		cin >> newNode->ch;

		int leftCount = nodeCount / 2;
		int rightCount = nodeCount - leftCount - 1;
		newNode->left = CreateTree(leftCount);
		newNode->right = CreateTree(rightCount);
		return newNode;
	}
}

void PrintTree(Node* root, int level)
{
	if (root != NULL)
	{
		PrintTree(root->right, level + 1);
		for (int i = 1; i <= level; i++)
			cout << "   ";
		cout << root->info;
		cout << root->ch << endl;
		PrintTree(root->left, level + 1);
	}
}

Info InfixOrder(Node* root, Info& min, char& ch)
{
	if (root != NULL)
	{
		InfixOrder(root->left, min, ch);
		cout << root->info << "";
		cout << root->ch << "   ";
		InfixOrder(root->right, min, ch);
		if (root->info < min) {
			min = root->info;
			ch = root->ch;
		}
	}
	return ch;
}
 
Share this answer
 
Comments
Richard MacCutchan 3-Jun-22 15:12pm    
Why do you keep ignoring what we have suggested, and continue to do it wrong?
Jonson123 2022 4-Jun-22 13:05pm    
sorry i did as merano99 and Patrice T suggested
Patrice T 3-Jun-22 15:35pm    
do not ask a question in an answer.
You should rather ask another question to get attenton.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900