Line 1: | Line 1: | ||
<br> | <br> | ||
− | = Binary Tree: In Order and Post Order = | + | = Binary Tree: In Order and Post Order = |
− | + | ||
− | + | ||
+ | ==== '''In Order Printing''' ==== | ||
+ | In order printing takes the root of a binary tree and prints all the values in the tree in order. The function is a recursive function that goes as far left in the binary tree until it hits the end. It will then print the leaf's value. After the leaf's value is printed, the function moves to the right once and precedes to go left until a leaf is found. Printing the leaf's value and continues on. | ||
+ | <br> | ||
− | Example code for in order printing: | + | Example code for in order printing: |
− | void Tree_inOrder(TNode *n) /*see declaration of TNode below*/ | + | void Tree_inOrder(TNode *n) /*see declaration of TNode below*/ |
− | { | + | { |
− | if(n==0) | + | if(n==0) |
− | return; | + | return; |
− | Tree_inOrder(n->left); | + | Tree_inOrder(n->left); |
− | printf("%d\n", n->value); | + | printf("%d\n", n->value); |
− | Tree_inOrder(n->right); | + | Tree_inOrder(n->right); |
− | } | + | } |
<br> | <br> | ||
− | + | ==== Post Order Printing ==== | |
− | + | Post order printing is similar to in order printing in the sense that all values of the tree are printed but is different in the order of which the values are printed. | |
− | | + | Post prder printing will take any node in the tree and print all the values to the left and right before the original node is printed. Example: If the root of the binary tree is 19 and post order printing is called with the root, then the very last line of output will be 19. |
− | + | <br> | |
− | + | Example code for in order printing: | |
− | + | void Tree_postOrder(TNode *n) /*see declaration of TNode below*/ | |
− | + | { | |
− | + | if(n==0) | |
+ | return; | ||
+ | Tree_postOrder(n->left); | ||
+ | Tree_postOrder(n->right); | ||
+ | printf("%d\n", n->value); | ||
+ | } | ||
+ | <br> | ||
+ | |||
+ | <br> | ||
+ | |||
+ | <br> | ||
− | The declaration of TNode is as followed: | + | The declaration of TNode is as followed: |
− | typedef struct Treenode | + | typedef struct Treenode |
− | { | + | { |
− | int value; | + | int value; |
− | struct Treenode *left; | + | struct Treenode *left; |
− | struct Treenode *right; | + | struct Treenode *right; |
− | }TNode; | + | }TNode; |
<br> [[ECE264|Back to ECE264]] | <br> [[ECE264|Back to ECE264]] | ||
[[Category:ECE264]] | [[Category:ECE264]] |
Revision as of 16:16, 28 April 2011
Binary Tree: In Order and Post Order
In Order Printing
In order printing takes the root of a binary tree and prints all the values in the tree in order. The function is a recursive function that goes as far left in the binary tree until it hits the end. It will then print the leaf's value. After the leaf's value is printed, the function moves to the right once and precedes to go left until a leaf is found. Printing the leaf's value and continues on.
Example code for in order printing:
void Tree_inOrder(TNode *n) /*see declaration of TNode below*/
{
if(n==0)
return;
Tree_inOrder(n->left);
printf("%d\n", n->value);
Tree_inOrder(n->right);
}
Post Order Printing
Post order printing is similar to in order printing in the sense that all values of the tree are printed but is different in the order of which the values are printed.
Post prder printing will take any node in the tree and print all the values to the left and right before the original node is printed. Example: If the root of the binary tree is 19 and post order printing is called with the root, then the very last line of output will be 19.
Example code for in order printing:
void Tree_postOrder(TNode *n) /*see declaration of TNode below*/
{
if(n==0)
return;
Tree_postOrder(n->left);
Tree_postOrder(n->right);
printf("%d\n", n->value);
}
The declaration of TNode is as followed:
typedef struct Treenode
{
int value;
struct Treenode *left;
struct Treenode *right;
}TNode;