Print out each element from a node in binary tree by using format of output

I have a question about a node in a binary tree. I saw some tutorials about this topic on the Internet. But the tutorial is quite simple. So I don't know how to deal with this problem.

So a node contains all information of a car's AVN system such as carName, carID in base class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Setting.h"

Setting::Setting()
{
	// Your code
	car_name = "";
	carID = ""; 
        general = NULL;//
	sound = NULL;
	disp = NULL;
}

Setting::~Setting(){
	if(general!=NULL)
		delete general;
	if (sound != NULL)
		delete sound;
	if (disp != NULL)
		delete disp;
}
string Setting::getCarName(){
	return car_name;
}

void Setting::setCarName(string data) {
	car_name = data;
}

void Setting::setCarID(string data) {
	car_id = data;
}

string Setting::getCarID(){
	return car_id;
}

void Setting:Input(){
        cout << "Car name: ";
	getline(cin, car_name);
        cout << "Car ID: ";
        getline(cin, car_id);
        sound->Input();
     	general->Input();
	disp->Input();

}

void Setting::Output(){
	cout << setw(20) << car_name << setw(25) << car_id;
}


Then I has three derivative classes of its such as Display,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Display.h"
using namespace std;

Display::Display()
{
	light_level = 0;
	screen_light_level = 0;
	taplo_light_level = 0;
}

Display::~Display(){}

int Display::get_light_level(){
	return light_level;
}

int Display::get_screen_light_level(){
	return screen_light_level;
}

int Display::get_taplo_light_level(){
	return taplo_light_level;
}

void Display::set_light_level(int data){
	light_level = data;
}

void Display::set_screen_light_level(int data){
	screen_light_level = data;
}

void Display::set_taplo_light_level(int data){
	taplo_light_level = data;
}

void Display::Input(){
	cout << "LIGHT LEVEL: ";
	cin >> light_level;
	
	cout << "TAPLO LIGHT LEVEL: ";
	cin >> taplo_light_level;

	cout << "SCREEN LIGHT LEVEL: ";
	cin >> screen_light_level;
}

void Display::Output() {
	cout << setw(10) << light_level << setw(10) << taplo_light_level << setw(10) << screen_light_level << endl;
}

Sound,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Sound::Sound(){
	media_level = 0;
	call_level = 0;
	navi_level = 0;
	notification_level = 0;
}

Sound::~Sound(){}

void Sound::Input(){
	cout << "MEDIA LEVEL: ";
	cin >> media_level;
	
	cout << "CALL LEVEL: ";
	cin >> call_level;
	
	cout << "NAVI LEVEL: ";
	cin >> navi_level;
	
	cout << "NOTIFICATION: ";
	cin >> notification_level;
}

void Sound::Output(){
	cout<< setw(10) << media_level << setw(10) << call_level << setw(10) << navi_level << setw(10) << notification_level << endl;
}

int Sound::get_media_level(){
	return media_level;
}

int Sound::get_call_level(){
	return call_level;
}

int Sound::get_navi_level(){
	return navi_level;
}

int Sound::get_notification_level(){
	return notification_level;
}

void Sound::set_media_level(int data){
	media_level = data;
}

void Sound::set_call_level(int data){
	call_level = data;
}

void Sound::set_navi_level(int data){
	navi_level = data;
}

void Sound::set_notification_level(int data){
	notification_level = data;;
}

and General.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
General::General()
{
	timeZone = "";
	language = "";
}

General::~General(){
}
void General::Input(){
	cin.ignore();
        cout<<"Your time zone: ";
	getline(cin, timeZone);
	cout << "your LANGUAGE: ";
	getline(cin, language);
}

void General::Output(){
	cout << setw(10) << timeZone << setw(10) << language << endl;
}

string General::get_language(){
	return language;
}

string General::get_timeZone(){
	return timeZone;
}

void General::set_timeZone(string data){
	timeZone = data;
}

void General::set_language(string data){
	language = data;
}



This is my Binary tree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include"Node.h"

using namespace std;

template<class T>

BST<T>::BST() {
	root = NULL;
}

template<class T>
Node<T>* BST<T>::newNode(T data)
{
	Node<T>* temp = new Node<T>();
	temp->data = data;
	temp->left = NULL;
	temp->right = NULL;
	return temp;
}

template<class T>
Node<T>* BST<T>::Insert(T data, Node<T>* aNode) {
	// the new element 
	Node* newnode = newNode(data);
	// Pointer to start traversing from root and traverses downward path to search where the new node to be inserted 
	Node* x = root;
	// Pointer y maintains the trailing pointer of x 
	Node* y = NULL;

	while (x != NULL) {
		y = x;
		if (data < x->data)
			x = x->left;
		else
			x = x->right;
	}
	// If the root is NULL i.e the tree is empty 
	// The new node is the root node 
	if (y == NULL)
		y = newnode;
	// If the new key is less then the leaf node key 
	// Assign the new node to be its left child 
	else if (data < y->data)
		y->left = newnode;
	// else assign the new node its right child 
	else
		y->right = newnode;

	// Returns the pointer where the new node is inserted 
	return y;
}

template<class T>
Node<T>* BST<T>::Search(T data, Node<T>* aNode) {
	if (aNode != NULL)
	{
		if (data == aNode->data);
		{
			return aNode;
		}
		if (data < aNode->data)
		{
			return Search(data, aNode->left);
		}
		else
		{
			return Search(data, aNode->right);
		}
	}
	else
	{
		return NULL;
	}
}

template<class T>
void BST<T>::inOrder() {
	inOrder(root);
}

template<class T>
void BST<T>::inOrder(Node<T>* aNode) {
	if (aNode != NULL)
	{
		inOrder(aNode->left);
		cout << aNode->data << " ";
		inOrder(aNode->right);
	}
}

template<class T>
int BST<T>::size(Node<T>* node)
{
	if (node == NULL)
		return 0;
	else
		return(size(node->left) + 1 + size(node->right));
}

I want to know how to access to each elements that I inserted to the node of binary tree to print it with my output function output

for example,
I inserted those information by using InputSetting() in main
1
2
3
4
5
6
7
8
9
10
11
12
int selection;
	char continues = 'n';
	do {
		Setting* stt = new Setting();
		setting.newNode(stt);
		stt->input();
		setting.Insert(stt);
		settingCnt++;
		cout << "Next car? y/n";
		cin >> continues;
		cout << endl;
	} while (continues == 'y');


Then what I should write in my printall() function
1
2
3
4
5
6
void printall() {
	int n = settingCnt;
	for (int i = 0; i < n; i++) {
		//Here I don't know how to get access to the elements I input to the node to print it out by using ouput() function format.
	}
}
Last edited on
1. What is a "derivative class"?
2. How do Setting, Display, Sound and General relate to each other?
3. What are you using as the template argument T in your BST?
4. Are you using the term "base class" correctly? Is it a base class (inheritance) or a container class?

Header files would have helped answer these question.

It looks like you are using Setting as the T value in your BST class. But I don't know that because I can't see your main(). If that is correct, using the name setting for a BST containing multiple Settings is confusing.

You have Output functions for all of your classes. I don't think you ever call them. Your BST::inOrder function calls the stream insertion operator (<<). You either need to have inOrder call Output or provide a stream insertion operator for Setting.

#doug4
Actually, there are not real "derivative class" here. Those classes are connected by using get/set function in my setting header.

The reason for using template for my binary tree is because I want to make it as general as possible so I could pass any type of data that I want (in this case is Setting*).

Let me show you my setting header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
ifndef SETTING_H_
#define SETTING_H_

#include <iostream>
#include <iomanip>
#include <string>
#include "General.h"
#include "Display.h"
#include "Sound.h"
#include<regex>


using namespace std;

class Setting {
public:
	Setting();
	virtual ~Setting();
	void nhapThongTin();
	void xuatThongTin();
	void xuatThongTin(int option);
	General* getGeneral() {
		return general;
	}
	Sound* getSound() {
		return sound;
	}
	Display* getDisp(){
		return disp;
	}
	void addGeneral(General* g)
	{
		general = g;
	}
	void addSound(Sound* s)
	{
		sound = s;
	}
	void addDisp(Display* d)
	{
		disp = d;
	}
	string getCarName();
	string getCarID();
	void setCarName(string data);
	void setCarID(string data);

private:
	string car_name;
	string carID; 	// Chuoi 8 ky tu so
	General *general; 
	Sound *sound;
	Display *disp;

};
#endif /* SETTING_H_ */ 


Last edited on
1. Why not just make general, sound and disp contained members of Setting? Why the pointers? You never seem to allocate them anyway (there is no general = new General; in your code, unless I missed it).

Instead, have
1
2
3
4
5
6
7
8
9
class Setting
{
public:
	General& getGeneral() {
		return general;
	}
private:
	General general;
};


Do this for all 3 members. This way, you never need to allocate the object or delete it. You won't need your destructor at all.

BTW, if you have a constructor that does nothing, you don't need to define it. Unless your class is doing some sort of dynamic memory allocation or obtaining resources, you don't need to declare a destructor. Look up the Rule of Three (now it's the Rule of Five) to see that if you declare a destructor, you should also declare copy constructor and assignment operator (and now "move" variants of those).

2. I see that you edited your initial post while I was responding. So, my last paragraph doesn't make as much sense any more.

Here is the code I am now responding to:

1
2
3
4
5
6
void printall() {
	int n = settingCnt;
	for (int i = 0; i < n; i++) {
		//Here I don't know how to get access to the elements I input to the node to print it out by using ouput() function format.
	}
}


1
2
3
4
5
6
7
8
9
template<class T>
void BST<T>::inOrder(Node<T>* aNode) {
	if (aNode != NULL)
	{
		inOrder(aNode->left);
		cout << aNode->data << " ";
		inOrder(aNode->right);
	}
}


Don't loop on the size of the tree. That's why you have the inOrder function. That function will traverse the whole tree, so there is no need to loop on the size.

To get your code to work right now, change line 6 of inOrder to aNode->data.output(); See what that gives you. You might need to add the blank space to the end of Data::output.

3. Eventually, in order to make this work with other types (AFTER YOU GET IT WORKING WITH YOUR CURRENT Output METHOD), restore line 6 of inOrder to the way you have it now and add the stream inserter function. This non-member function may need to be a friend function of the class, but it is declared as:

in the class:
friend std::ostream& operator<<(std::ostream& out, Setting& setting);

outside of the class:
std::ostream& operator<<(std::ostream& out, Setting& setting);

definition:
1
2
3
4
5
6
7
8
9
std::ostream& operator<<(std::ostream &out, Setting& setting)
{
	out << setw(20) << car_name << setw(25) << car_id;
	out << general;
	out << sound;
	out << disp;
	out << " ";
	return out;
}


This last code snippet depends on you defining the stream insertion operator for all of the contained classes, too.

Topic archived. No new replies allowed.