Cout a binary tree (sorted)

So basically my program asks how many random numbers does the user want to create; then it creates that number of random numbers in a linked list, then it show the numbers and it transforms it into a binary tree so it is sorted in a logical way.

Now I want to turn that binary tree into a sorted linked list, but I don't know how to do it... for now I've just been able to show the smallest number...

PD: I know that there are better ways of sorting it, but I want to do it this way. THANKS!

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
 #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
struct node
{
	int key_value;
	node *p_left;
	node *p_right;
};
struct number
{
    int random_number;
    number* p_next_number;
};

number* p_list_numbers = NULL;
number* p_first_number = NULL;
node* p_middle = NULL;

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;
		return p_new_tree;
	}
	
	if( key < p_tree->key_value )
	{
		
		p_tree->p_left = insert_node( p_tree->p_left, key );
	}
	else
	{
		
		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->p_left != NULL)
    {
        show_sorted_list(p_root->p_left);
    }
    else
    cout<< "\n\nThis are the sorted numbers"<< p_root->key_value;
    
    
}

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;
    }
    p_middle = 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.