function to find inorder BST

Given input
34 74 32 23 33 72 81 6

output should be
81 74 72 34 33 32 23 6


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

#include "stdafx.h"
#include <iostream>

using namespace std;
//typedef double T;

struct tnode {
	int data;
	tnode *lchild, *rchild;
};

class tree {
public:
	tree();
	void nlrscan();
private:
	tnode *root;
	tnode *build();
	void nlrscan(tnode *r);
};

tree::tree() {
	cout << "Input the tree in NLR format:";
	root = build();
}
tnode* tree::build() {
	char c;
	cin >> c;
	if (c == '.')
		return NULL;
	tnode *t;
	t = new(tnode);
	t->data = c;
	t->lchild = build();
	t->rchild = build();
	return t;
}
void tree::nlrscan() {
	cout << "NLR scan of this tree is as follows:";
	nlrscan(root);
	cout << endl;
}
void tree::nlrscan(tnode *r) {
	if (r) {
		nlrscan(r->lchild);
		cout << r->data << " ";
		nlrscan(r->rchild);
	}
	}

int _tmain(int argc, _TCHAR* argv[])
{
	tree ex1;
	ex1.nlrscan();
	return 0;
}
Hello csharp,

Just a note for the future. #include "stdafx" . Some people will tell you that this is not a standard C++ header file and should not be there. It is best to delete this line when posting your code. Personally it makes no difference to me.

When I first compiled the program I received errors because of int _tmain(int argc, _TCHAR* argv[]). This may work for the way you have setup the program/project, but not for everyone. I had to change this to
int main(int argc, char* argv[]).

When I ran the program the "build" function does not work well. Using a "char" for input, lines 28 and 29 works for detecting the period, but does not store your numbers properly. Also I had to enter about 15 periods to end the input. The "build" function tends to be a recursive loop with lines 35 and 36 thus the need for more than one period to end the input.

I had to put a break point on line 56 just to see the output before the window closed. The numbers I saw were not the numbers I entered, so it was hard to tell if they were in the right order.

The program needs some work, but I am not sure which way to go yet.

Hope that helps,

Andy
output should be
81 74 72 34 33 32 23 6

Why? You don’t put any condition to determine where every ‘data’ should go, so they are saved in insertion order.
Have a look at what happens inside your code:
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
#include <iomanip>
#include <iostream>

struct tnode {
    int data {};
    tnode *lchild { nullptr }, *rchild { nullptr };
};

class tree {
public:
    tree();
    void nlrscan();
private:
    tnode *root;
    tnode *build();
    void nlrscan(tnode *r);
};

tree::tree()
{
    std::cout << "Input the tree in NLR format:\n";
    root = build();
}

tnode* tree::build()
{
    std::cout << "(-1 to exit) >>> ";
    int i {};
    std::cin >> i;
    if (i == -1) { return nullptr; }
    tnode* t = new tnode;
    t->data = i;
    std::cout << "Going left...\n";
    t->lchild = build();
    std::cout << "Going right...\n";
    t->rchild = build();
    return t;
}

void tree::nlrscan()
{
    std::cout << "NLR scan of this tree is as follows:";
    nlrscan(root);
    std::cout << '\n';
}

void tree::nlrscan(tnode *r)
{
    if (r) {
        if(r->lchild) {
            std::cout << " --> left";
            nlrscan(r->lchild);
        }
        std::cout << " --> data: " << r->data;
        if(r->rchild) {
            std::cout << " --> right: ";
            nlrscan(r->rchild);
        }
    }
}

int main()
{
    tree ex1;
    ex1.nlrscan();
    return 0;
}

Output:
Input the tree in NLR format:
(-1 to exit) >>> 34
Going left...
(-1 to exit) >>> 74
Going left...
(-1 to exit) >>> 32
Going left...
(-1 to exit) >>> 23
Going left...
(-1 to exit) >>> 33
Going left...
(-1 to exit) >>> 72
Going left...
(-1 to exit) >>> 81
Going left...
(-1 to exit) >>> 6
Going left...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
Going right...
(-1 to exit) >>> -1
NLR scan of this tree is as follows: --> left --> left --> left --> left --> 
left --> left --> left --> data: 6 --> data: 81 --> data: 72 --> data: 33 --> 
data: 23 --> data: 32 --> data: 74 --> data: 34

Topic archived. No new replies allowed.