negative numbers in Binary Tree












-3















I want to multiply each node with negative value -2 in Binary Tree. But I do not know how to implement it. When multiplying with negative number left and right sub trees change their place. I got stuck at doing this.



 typedef struct BTree {
int value;
struct BTree *left, *right;
} BTree;

BTree *insert(BTree *root, int value) {
if (root == NULL) {
BTree *new_node = (BTree*) malloc(sizeof(BTree));
new_node->value = value;
new_node->left = new_node->right = NULL;
return new_node;
}
if (value < root->value) {
root->left = insert(root->left, value);
}
else if (value > root->value) {
root->right = insert(root->right, value);
}
else {

}
return root;
}

void print_tree(BTree *root)
{
if (root == NULL) return;
print_tree(root->left);
printf("%d ", root->value);
print_tree(root->right);
}



void swap_tree(BTree *root)
{
if (root == NULL)
return;
else
{
BTree *temp;

swap_tree(root->left);
swap_tree(root->right);

temp = root->left;
root->left = root->right;
root->right = temp;
}
}









share|improve this question

























  • You want to multiply a node? You mean, your nodes contain values and you want to multiply those values? Please edit your question and include your code so we can have a better look.

    – Blaze
    Nov 23 '18 at 9:10













  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 9:12











  • (BTree*) malloc(sizeof(BTree)); typecast is needless.

    – WedaPashi
    Nov 23 '18 at 9:30
















-3















I want to multiply each node with negative value -2 in Binary Tree. But I do not know how to implement it. When multiplying with negative number left and right sub trees change their place. I got stuck at doing this.



 typedef struct BTree {
int value;
struct BTree *left, *right;
} BTree;

BTree *insert(BTree *root, int value) {
if (root == NULL) {
BTree *new_node = (BTree*) malloc(sizeof(BTree));
new_node->value = value;
new_node->left = new_node->right = NULL;
return new_node;
}
if (value < root->value) {
root->left = insert(root->left, value);
}
else if (value > root->value) {
root->right = insert(root->right, value);
}
else {

}
return root;
}

void print_tree(BTree *root)
{
if (root == NULL) return;
print_tree(root->left);
printf("%d ", root->value);
print_tree(root->right);
}



void swap_tree(BTree *root)
{
if (root == NULL)
return;
else
{
BTree *temp;

swap_tree(root->left);
swap_tree(root->right);

temp = root->left;
root->left = root->right;
root->right = temp;
}
}









share|improve this question

























  • You want to multiply a node? You mean, your nodes contain values and you want to multiply those values? Please edit your question and include your code so we can have a better look.

    – Blaze
    Nov 23 '18 at 9:10













  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 9:12











  • (BTree*) malloc(sizeof(BTree)); typecast is needless.

    – WedaPashi
    Nov 23 '18 at 9:30














-3












-3








-3








I want to multiply each node with negative value -2 in Binary Tree. But I do not know how to implement it. When multiplying with negative number left and right sub trees change their place. I got stuck at doing this.



 typedef struct BTree {
int value;
struct BTree *left, *right;
} BTree;

BTree *insert(BTree *root, int value) {
if (root == NULL) {
BTree *new_node = (BTree*) malloc(sizeof(BTree));
new_node->value = value;
new_node->left = new_node->right = NULL;
return new_node;
}
if (value < root->value) {
root->left = insert(root->left, value);
}
else if (value > root->value) {
root->right = insert(root->right, value);
}
else {

}
return root;
}

void print_tree(BTree *root)
{
if (root == NULL) return;
print_tree(root->left);
printf("%d ", root->value);
print_tree(root->right);
}



void swap_tree(BTree *root)
{
if (root == NULL)
return;
else
{
BTree *temp;

swap_tree(root->left);
swap_tree(root->right);

temp = root->left;
root->left = root->right;
root->right = temp;
}
}









share|improve this question
















I want to multiply each node with negative value -2 in Binary Tree. But I do not know how to implement it. When multiplying with negative number left and right sub trees change their place. I got stuck at doing this.



 typedef struct BTree {
int value;
struct BTree *left, *right;
} BTree;

BTree *insert(BTree *root, int value) {
if (root == NULL) {
BTree *new_node = (BTree*) malloc(sizeof(BTree));
new_node->value = value;
new_node->left = new_node->right = NULL;
return new_node;
}
if (value < root->value) {
root->left = insert(root->left, value);
}
else if (value > root->value) {
root->right = insert(root->right, value);
}
else {

}
return root;
}

void print_tree(BTree *root)
{
if (root == NULL) return;
print_tree(root->left);
printf("%d ", root->value);
print_tree(root->right);
}



void swap_tree(BTree *root)
{
if (root == NULL)
return;
else
{
BTree *temp;

swap_tree(root->left);
swap_tree(root->right);

temp = root->left;
root->left = root->right;
root->right = temp;
}
}






c binary-tree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:38







Emil Bədrəddinli

















asked Nov 23 '18 at 9:08









Emil BədrəddinliEmil Bədrəddinli

256




256













  • You want to multiply a node? You mean, your nodes contain values and you want to multiply those values? Please edit your question and include your code so we can have a better look.

    – Blaze
    Nov 23 '18 at 9:10













  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 9:12











  • (BTree*) malloc(sizeof(BTree)); typecast is needless.

    – WedaPashi
    Nov 23 '18 at 9:30



















  • You want to multiply a node? You mean, your nodes contain values and you want to multiply those values? Please edit your question and include your code so we can have a better look.

    – Blaze
    Nov 23 '18 at 9:10













  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 9:12











  • (BTree*) malloc(sizeof(BTree)); typecast is needless.

    – WedaPashi
    Nov 23 '18 at 9:30

















You want to multiply a node? You mean, your nodes contain values and you want to multiply those values? Please edit your question and include your code so we can have a better look.

– Blaze
Nov 23 '18 at 9:10







You want to multiply a node? You mean, your nodes contain values and you want to multiply those values? Please edit your question and include your code so we can have a better look.

– Blaze
Nov 23 '18 at 9:10















Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.

– Some programmer dude
Nov 23 '18 at 9:12





Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.

– Some programmer dude
Nov 23 '18 at 9:12













(BTree*) malloc(sizeof(BTree)); typecast is needless.

– WedaPashi
Nov 23 '18 at 9:30





(BTree*) malloc(sizeof(BTree)); typecast is needless.

– WedaPashi
Nov 23 '18 at 9:30












1 Answer
1






active

oldest

votes


















1














From your question it seems you are talking about binary search tree and not just a binary tree. As you correctly noted multiplying all the values in the nodes of a binary search tree will lead to changing the order of the subtrees at each node. How you implement that will depend on the tree representation that you are using, but for most a recursion based approach that swaps the two child nodes at each node starting from the leafs should work.






share|improve this answer
























  • There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

    – Eric Postpischil
    Nov 23 '18 at 9:17











  • @EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

    – Ivaylo Strandjev
    Nov 23 '18 at 9:20











  • “Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

    – Eric Postpischil
    Nov 23 '18 at 9:25













  • @EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

    – Ivaylo Strandjev
    Nov 23 '18 at 9:34






  • 1





    @EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

    – Ivaylo Strandjev
    Nov 23 '18 at 9:39












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443577%2fnegative-numbers-in-binary-tree%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














From your question it seems you are talking about binary search tree and not just a binary tree. As you correctly noted multiplying all the values in the nodes of a binary search tree will lead to changing the order of the subtrees at each node. How you implement that will depend on the tree representation that you are using, but for most a recursion based approach that swaps the two child nodes at each node starting from the leafs should work.






share|improve this answer
























  • There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

    – Eric Postpischil
    Nov 23 '18 at 9:17











  • @EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

    – Ivaylo Strandjev
    Nov 23 '18 at 9:20











  • “Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

    – Eric Postpischil
    Nov 23 '18 at 9:25













  • @EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

    – Ivaylo Strandjev
    Nov 23 '18 at 9:34






  • 1





    @EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

    – Ivaylo Strandjev
    Nov 23 '18 at 9:39
















1














From your question it seems you are talking about binary search tree and not just a binary tree. As you correctly noted multiplying all the values in the nodes of a binary search tree will lead to changing the order of the subtrees at each node. How you implement that will depend on the tree representation that you are using, but for most a recursion based approach that swaps the two child nodes at each node starting from the leafs should work.






share|improve this answer
























  • There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

    – Eric Postpischil
    Nov 23 '18 at 9:17











  • @EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

    – Ivaylo Strandjev
    Nov 23 '18 at 9:20











  • “Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

    – Eric Postpischil
    Nov 23 '18 at 9:25













  • @EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

    – Ivaylo Strandjev
    Nov 23 '18 at 9:34






  • 1





    @EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

    – Ivaylo Strandjev
    Nov 23 '18 at 9:39














1












1








1







From your question it seems you are talking about binary search tree and not just a binary tree. As you correctly noted multiplying all the values in the nodes of a binary search tree will lead to changing the order of the subtrees at each node. How you implement that will depend on the tree representation that you are using, but for most a recursion based approach that swaps the two child nodes at each node starting from the leafs should work.






share|improve this answer













From your question it seems you are talking about binary search tree and not just a binary tree. As you correctly noted multiplying all the values in the nodes of a binary search tree will lead to changing the order of the subtrees at each node. How you implement that will depend on the tree representation that you are using, but for most a recursion based approach that swaps the two child nodes at each node starting from the leafs should work.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 9:13









Ivaylo StrandjevIvaylo Strandjev

56.9k1188144




56.9k1188144













  • There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

    – Eric Postpischil
    Nov 23 '18 at 9:17











  • @EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

    – Ivaylo Strandjev
    Nov 23 '18 at 9:20











  • “Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

    – Eric Postpischil
    Nov 23 '18 at 9:25













  • @EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

    – Ivaylo Strandjev
    Nov 23 '18 at 9:34






  • 1





    @EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

    – Ivaylo Strandjev
    Nov 23 '18 at 9:39



















  • There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

    – Eric Postpischil
    Nov 23 '18 at 9:17











  • @EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

    – Ivaylo Strandjev
    Nov 23 '18 at 9:20











  • “Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

    – Eric Postpischil
    Nov 23 '18 at 9:25













  • @EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

    – Ivaylo Strandjev
    Nov 23 '18 at 9:34






  • 1





    @EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

    – Ivaylo Strandjev
    Nov 23 '18 at 9:39

















There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

– Eric Postpischil
Nov 23 '18 at 9:17





There is no need for recursion. If the tree includes limks to parents, then a traversal is simple without recursion. If it does not, a traversal only requires an array of pointers to nodes in the current path from the root, with as many elements as the tree depth.

– Eric Postpischil
Nov 23 '18 at 9:17













@EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

– Ivaylo Strandjev
Nov 23 '18 at 9:20





@EricPostpischil having a look at the structure that OP now included in the question I still believe recursion is the most natural approach

– Ivaylo Strandjev
Nov 23 '18 at 9:20













“Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

– Eric Postpischil
Nov 23 '18 at 9:25







“Natural”? We should use more resources than necessary because it is “natural”? Do you mean “easy” or “low effort”? What is gained by using a “natural” approach?

– Eric Postpischil
Nov 23 '18 at 9:25















@EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

– Ivaylo Strandjev
Nov 23 '18 at 9:34





@EricPostpischil feel free to add another answer with the approach you propose, I don't believe these comments will lead to any constructive discussion that will lead to improving the quality of my answer

– Ivaylo Strandjev
Nov 23 '18 at 9:34




1




1





@EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

– Ivaylo Strandjev
Nov 23 '18 at 9:39





@EmilBədrəddinli this should be pretty straight forward - simply multiple the node's value by -2.

– Ivaylo Strandjev
Nov 23 '18 at 9:39




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443577%2fnegative-numbers-in-binary-tree%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]