0

I was reading about trees and a possible implementation of the 3 traversal functions (from : Data Structures Using C and C++). The Node struct is :

struct node
{
    int info;
    node* left;
    node* right;
}node;

typedef struct node * nodeptr ;

The implementation of Preorder traverse took a simple pointer of type node:

void preorder(node *root);

But when it came to Inorder function, it was declared as following :

void inorder(nodeptr *root)

If I'm not mistaken, and since I looked it up online, in this case nodeptr* would mean ** node right ? If that's the case, why are we declaring a double pointer of type node for a traverse function ?

Another thing,

inorder(root→left);

if root is a double pointer of type node, can we actually write root->left to access the attributes of the node it points to ? (as if it were a simple pointer) ?

Edit: I'm just copying what was written in the book and what was given in class. I just had a hard time understanding why, that's all.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
lastmaj
  • 146
  • 2
  • 8
  • 1
    Do not spend your time investigating bad programs.:) – Vlad from Moscow Oct 16 '17 at 13:26
  • 1
    Look at the implementation of it. For example some implementations of linked lists are working with `**` in order to avoid some extra boundary condition checks. – Eugene Sh. Oct 16 '17 at 13:26
  • BTW `typedef struct node * nodeptr ;` the typedef can be considered bad style (hiding a pointer behind a typedef) The pointer-to-pointer is also not needed here, since the function does not need to change the tree (or its root pointer) Final question: `&(*pp)->left` – joop Oct 16 '17 at 13:29
  • `void inorder(nodeptr *root)` means `void inorder(struct node **root)` – Gor Asatryan Oct 16 '17 at 13:30
  • Looks like it's a typo and they should have written `node *` or `nodeptr`. – interjay Oct 16 '17 at 13:32
  • @Ahmed Mejbri It is not a rare case when a book is written by an author who is unable to write programs or demonstrates a bad style of programming in his books.:) Moreover book code examples can contain typos. – Vlad from Moscow Oct 16 '17 at 13:32
  • Thank you all guys, I might as well just drop it and write mine. joop thanks for answering that last bit :D – lastmaj Oct 16 '17 at 13:36
  • 2
    There is no type `node` defined in your snippet. – Gerhardh Oct 16 '17 at 13:57
  • 1
    Without seeing the implementations of `preorder()` and `inorder()`, it's anybody's guess why they take different argument types. Normally you'd only pass a pointer to a pointer if you wanted the function to write a new pointer value to the parameter, but I can't think of a reason why `inorder` would need to do so if `preorder` doesn't. It's either a typo (they happen) or a badly-written example (they happen, too, all too often). As for the second question, if `root` has type `node **`, then you'd access the `left` member as `(*root)->left` or `(**root).left`. – John Bode Oct 16 '17 at 14:05
  • This link can enlighten you: https://www.linkedin.com/pulse/linus-torvalds-double-pointer-approach-rohan-verma/?articleId=8534015600813880253 – klutt Oct 16 '17 at 14:11
  • @VladfromMoscow There are circumstances where this is a very good thing. – klutt Oct 16 '17 at 14:17

0 Answers0