convert int to double

hi. could you please help me convert int to double in the following 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
 #include <vector>
 
using namespace std;
struct Tree
{
    int data;
    struct Tree* left, * right;
};
 
struct Tree* newtree(int key)
{
    struct Tree* temp = new Tree;
    temp->data = key;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
Tree* insert(Tree* tree, int key)
{
    if (tree == NULL) return newtree(key);
    if (key < tree->data)
        tree->left = insert(tree->left, key);
    else
        tree->right = insert(tree->right, key);
    return tree;
}
void store(Tree* root, int a[], int& i)
{
    if (root != NULL)
    {
        store(root->left, a, i);
        a[i++] = root->data;
        store(root->right, a, i);
    }
}
 
void TreeSort(vector<int>& a)
{
    struct Tree* root = NULL;
    root = insert(root, a[0]);
    for (size_t i = 1; i < a.size(); i++)
        insert(root, a[i]);
    int i = 0;
    store(root, a.data(), i);
}
Which int?
just so my code could work with doubles
Last edited on
Hello oksaannna,

Do you mean:
1
2
3
4
5
struct Tree
{
    int data;
    struct Tree* left, * right;
};


to:
1
2
3
4
5
struct Tree
{
    double data;
    struct Tree* left, * right;
};


Andy
Something like (not tried):

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 <vector>
using namespace std;

using mytype = double;

struct Tree
{
	mytype data {};
	Tree *left {}, *right {};

	Tree(mytype key) : data(key) {}
};

Tree* newtree(mytype key)
{
	return new Tree {key};
}

Tree* insert(Tree* tree, mytype key)
{
	if (tree == nullptr)
		return newtree(key);

	if (key < tree->data)
		tree->left = insert(tree->left, key);
	else
		tree->right = insert(tree->right, key);

	return tree;
}

void store(Tree* root, vector<mytype>& a)
{
	if (root != nullptr) {
		store(root->left, a);
		a.push_back(root->data);
		store(root->right, a);
	}
}

void TreeSort(vector<mytype>& a)
{
	Tree* root {};

	for (const auto& aa : a)
		insert(root, aa);

        a.clear();
	store(root, a);
}


where mytype can be changed to the desired type.

Also, when using Tree as a type, you don't need to precede by struct (that's the c way - not C++)

As you using vector, store should take a vector and not an array/index.

Use nullptr and not NULL (that's c again).
Last edited on
Search through the code for "int"

Everywhere you find an int, look at how it's used. Decided if it should stay as an int or change to a double. Sometimes "int" is the data in the tree. Other times "int" is the the number of items in an array. You want to change the ones that represent data in the tree, but not the ones that represent the number of items in an array.
> hi. could you please help me convert int to double in the following code?
Smells like "I found this code but I'm clueless because google is my only skill".
https://www.geeksforgeeks.org/tree-sort/
Topic archived. No new replies allowed.