Binary Search Tree
A binary-tree in which all the items in the left subtree are smaller than the root
item and all the items in the right subtree are larger than the root
item, is called a binary-search-tree.
Traversing a Binary Tree
Imagine the left subtree, right subtree and root node labeled as L
, R
and N
respectively.
Then, the traversal of the form:
1. (N, L, R) is called pre-order-traversal.
2. (L, N, R) is called in-order-traversal.
3. (L, R, N) is called post-order
C++ Code
void preorder(TreeNode<int>* treeNode) {
if(treeNode != NULL) {
cout << *(treeNode->getInfo())<<" ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
void inorder(TreeNode<int>* treeNode) {
if(treeNode != NULL) {
inorder(treeNode->getLeft());
cout << *(treeNode->getInfo())<<" ";
inorder(treeNode->getRight());
}
}
void postorder(TreeNode<int>* treeNode) {
if(treeNode != NULL) {
postorder(treeNode->getLeft());
postorder(treeNode->getRight());
cout << *(treeNode->getInfo())<<" ";
}
}