This is a split board - You can return to the Split List for other boards.

C Programming Question (Binary Search Tree Recursion)

#1Treason686Posted 11/18/2013 5:36:02 PM
I should probably post this elsewhere, but I'm sure someone around here knows the answer.

I recently finished a project in school and didn't quite understand some of the syntax.

The program was for a binary search tree and uses recursive calls to add nodes to the tree. I understand the tree concept well, but the recursive call has been bugging me. This is the setup in main:

NodeT *root1;
insert(&root1, itemToBeInserted);


And in the insert function:

void insert(NodeT **tree, NodeItemT itemToBeInserted)
{
//This is the recursive call syntax
insert(&((*tree)->right), itemToBeInserted);
}


So in main, I'm sending the address of the pointer. In the insert function, I have a pointer to a pointer of the address? Really, the whole thing is just blowing my mind.
---
PC: Core i7 920 || 6GB DDR3 || GTX 660 Ti || HP w2408h
#2KotickIsKingPosted 11/18/2013 6:25:21 PM
stackoverflow.com

sorry, it's for the best.
---
CPU: Intel Core i17 32 cores @ 7.50 EHz GPU: NVIDIA GeForce 39980 GTX SSD: OCZ Agili7y 256ZB
Ram: 4096 EB @ 9001 EHz PSU: 1,000,000 W
#3GwynsSonSolairePosted 11/18/2013 6:30:48 PM
what language is this? python?
---
Official TimePharaoh Fanboy #1
Put this in your sig if you are a TimePharaoh fanboy
#4Treason686(Topic Creator)Posted 11/18/2013 6:43:09 PM
KotickIsKing posted...
stackoverflow.com

sorry, it's for the best.


That was my next stop.

GwynsSonSolaire posted...
what language is this? python?


Try again.
---
PC: Core i7 920 || 6GB DDR3 || GTX 660 Ti || HP w2408h
#5squeeb1985Posted 11/18/2013 6:48:03 PM
Why the heck are you passing in a pointer to a pointer just to dereference it? I mean,

void insert(NodeT *tree, NodeItemT itemToBeInserted)
{
//This is the recursive call syntax
if(tree != NULL)
insert(tree->right, itemToBeInserted);
}


should work just fine.
---
i5 3570k @ 4.4GHz | 6950 2GB | 8GB 1600MHz DDR3 | 840 Pro 256 GB | Win8 Pro 64
GT: Beastwood5
#6FoolInTheWavePosted 11/18/2013 6:59:23 PM(edited)
You're going to insert the new node in the rightmost position in the tree that has a null left child. The recursive call is to continue down the tree until you find a parent node that has a null left child.

Ah, C++, it's a good language, but the syntax is hard on purpose.

Edited post for correctness, but it probably still doesn't make any sense.
#7Treason686(Topic Creator)Posted 11/19/2013 5:15:40 AM
squeeb1985 posted...
Why the heck are you passing in a pointer to a pointer just to dereference it? I mean,

void insert(NodeT *tree, NodeItemT itemToBeInserted)
{
//This is the recursive call syntax
if(tree != NULL)
insert(tree->right, itemToBeInserted);
}


should work just fine.


A header file and testing main was supplied by the professor. We were not allowed to modify it.

I understand the concept. The question was in interpreting the syntax when doing the recursive call.
---
PC: Core i7 920 || 6GB DDR3 || GTX 660 Ti || HP w2408h
#8JudgmenlPosted 11/19/2013 5:29:27 AM
The people in this thread are so bad at this they can't even recognize this is C (and it's clearly marked in the title).

BRB, I have to read this and figure out what you're actually doing...
---
http://i.imgur.com/sUtdyzN.png - Popularity =/= Quality.
http://i.imgur.com/0Awd55E.png - Opinions =/= Truth.
#9JudgmenlPosted 11/19/2013 5:32:31 AM
insert(&((*tree)->right), itemToBeInserted);

What does this function do?
---
http://i.imgur.com/sUtdyzN.png - Popularity =/= Quality.
http://i.imgur.com/0Awd55E.png - Opinions =/= Truth.