Smerald Guci
Smerald Guci

Reputation: 25

Count occurrences in binary tree in C

struct tree{
    struct node* root;
};

typedef struct tree* Tree;

struct node{
   int key;
   struct node* left;
           struct node* right;
};
typedef struct node* node;

My problem is that the function I need to implement requires as a parameter a Tree and not a Node.

int count_occurrences(Tree t, int k){}

I know how to implement this function if the first parameter was of type Node, but since it needs a type Tree I can't figure it out how to pass the type Tree parameter in the recursive calls.

EDIT: Also there is another problem. I can't access struct fields directly since they are declared in another file (school project). I have access to some functions like getting the root of a tree, left or right child of a node etc

Tree newtree();
int treeempty(Tree t);
Node root(Tree t);
int readInfo(Node v);
void setInfo(Node v, int x);
Node leftChild(Node v);
Node rightChild(Node v);

Upvotes: 0

Views: 770

Answers (1)

NPE
NPE

Reputation: 500407

One straightforward way is to create a helper function:

int count_occurrences_helper(node d, int k) {
  /* You already know how to implement this. */
}

int count_occurrences(Tree t, int k) {
  return count_occurrences_helper(t->root, k);
}

Upvotes: 2

Related Questions