Please help me with this function!!!!

I've been trying to make a sorted list with x random numbers (the user chooses how many random numbers), print the random numbers, then sort those numbers by making a binary tree and finally print the sorted numbers.

For now I've just been able to reach to the point of making the binary tree and showing the smallest number, but I can't figure out how to print the rest of the numbers.

I have been trying new things, and this is how my code is atm, it always gives an error since it calls itself infinitely...

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
struct node
{
	int key_value;
	node *p_left;
	node *p_right;
	node *p_before;

};
struct number
{
    int random_number;
    number* p_next_number;
};
node* p_number_before = NULL;
number* p_list_numbers = NULL;
number* p_first_number = NULL;
int max_number = 0;

int find_max_number(node* p_root)
{
    if (p_root->p_right != NULL)
    {
        find_max_number(p_root->p_right);
    }
    return p_root->key_value;
}

node* insert_node (node *p_tree, int key)
{
	
	if ( p_tree == NULL )
	{
		node* p_new_tree = new node;
		p_new_tree->p_left = NULL;
		p_new_tree->p_right = NULL;
		p_new_tree->key_value = key;
		p_new_tree->p_before = p_number_before;
		return p_new_tree;
	}
	
	if( key < p_tree->key_value )
	{
		
		p_number_before = p_tree;
		p_tree->p_left = insert_node( p_tree->p_left, key );
	}
	else
	{
		
		p_number_before = p_tree;
		p_tree->p_right = insert_node( p_tree->p_right, key );
	}
	return p_tree;
}

number* get_new_number(int i)
{
    number* p_number = new number;
    p_number->random_number = i;
    p_number->p_next_number = p_list_numbers;
    p_list_numbers = p_number;
    return p_number;
}

void show_sorted_list(node* p_root)
{
    if (p_root != NULL)
    {
        while (p_root->p_left != NULL)
    {
        p_root = p_root->p_left;
    }
    cout<<  p_root->key_value<<", ";
    show_sorted_list(p_root->p_right);
    }
     else if (p_root->p_before->key_value == max_number)
    {
        cout<<".";
    }
    else
        cout<< "I DON'T KNOW WHAT TO PUT HERE!";
}

void show_numbers(int i)
{
    int count_numbers = 0;
    p_first_number = p_list_numbers;
    while (p_list_numbers != NULL)
    {
       cout<< p_list_numbers->random_number;
       if (count_numbers < (i-1))
       {
           cout<< ", ";
       } else
       cout << ".";
       p_list_numbers = p_list_numbers->p_next_number;
       count_numbers++;
    }
}

void order_numbers(int times, number* p_first)
{
    node* p_root = NULL;
    int node_value = 0;
    for (int i = 0; i < times; i++)
    {
        node_value = p_first->random_number;
        p_root = insert_node (p_root, node_value);
        p_first = p_first->p_next_number;
    }
    max_number = find_max_number(p_root);
    show_sorted_list(p_root);

}


int main()
{
    long int times;
    cout<<"How many numbers do you wish me to order? ";
    cin>>times;

    srand(time(NULL));

    for (int i = 0; i < times; i++)
    {
        int random = rand() % 100;
        get_new_number (random);
    }
    cout<< "These are the original numbers: ";
    show_numbers(times);
    p_list_numbers = p_first_number;
    order_numbers(times, p_first_number);


}

Last edited on
Topic archived. No new replies allowed.