Pre-Order Traversal
In this lesson, we will cover the traversal strategy, 'Pre-Order Traversal' in a Binary Search Tree, and implement it in C++.
We'll cover the following...
Introduction #
In this traversal, the elements are traversed in the root-left-right
order. We first visit the root/parent node, then the left child, and then the right child. Here is a high-level description of the algorithm for Pre-Order traversal, starting from the root node:
-
Visit the current node, i.e., print the value stored at the node
-
Call the
preOrderPrint()
function on the left sub-tree of thecurrent node
. -
Call the
preOrderPrint()
function on the right sub-tree of thecurrent node
.
Implementation in C++
...Press + to interact
void preOrderPrint(Node* currentNode) {if(currentNode!=NULL) {cout<<currentNode->value<<endl;preOrderPrint(currentNode->leftChild);preOrderPrint(currentNode->rightChild);}}
Yes, ...
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy