Insert to Binary Tree
- Let's design a structure
struct Node
{
int value;
Node* pLeft;
Node* pRight;
};
- Design a function
insertToTree(), which will
recursively
insert smaller value in its left sub-tree, and larger (or equal) values
in its right sub-tree.
- Compared with the linked list data
structure, the advantage of this approach is that pHead need not be changed.
- Design a function printTree(p), which recursively prints
- its left subtree,
- its value, and
- its right subtree.
- The results will be a sorted sequence.
The program may run as follows:
9 8 7 5 3 2 0
2 3 5 7 8 9