Pre-condition:
typedef struct intnode {
int info;
node *left, *right;
}node;
Algorithm:step 1: if pointer is null, return;
step 2: visit left sub-tree
step 3: visit root
step 4: visit right sub-tree
step 5: stop
Code:
void inorder(node *p) {
// if p is null return
if( p == null )
return;
// left sub-tree
inorder(p->left);
// root
printf("%d", p->info);
//right sub-tree
inorder(p->right);
}
Complexity:O(n), because each node has to be visited atleast once and increases linearly for increasing 'n'.
No comments :
Post a Comment