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

C Programming Question (Binary Search Tree Recursion)

  • Topic Archived
You're browsing the GameFAQs Message Boards as a guest. Sign Up for free (or Log In if you already have an account) to be able to post messages, change how messages are displayed, and view media in posts.
  1. Boards
  2. PC
  3. C Programming Question (Binary Search Tree Recursion)

User Info: Treason686

Treason686
3 years ago#1
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

User Info: KotickIsKing

KotickIsKing
3 years ago#2
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

User Info: GwynsSonSolaire

GwynsSonSolaire
3 years ago#3
what language is this? python?
Official TimePharaoh Fanboy #1
Put this in your sig if you are a TimePharaoh fanboy

User Info: Treason686

Treason686
3 years ago#4
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

User Info: squeeb1985

squeeb1985
3 years ago#5
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

User Info: FoolInTheWave

FoolInTheWave
3 years ago#6
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.

User Info: Treason686

Treason686
3 years ago#7
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

User Info: Judgmenl

Judgmenl
3 years ago#8
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.

User Info: Judgmenl

Judgmenl
3 years ago#9
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.
  1. Boards
  2. PC
  3. C Programming Question (Binary Search Tree Recursion)

Report Message

Terms of Use Violations:

Etiquette Issues:

Notes (optional; required for "Other"):
Add user to Ignore List after reporting

Topic Sticky

You are not allowed to request a sticky.

  • Topic Archived