2018-10-25
We're going to continue talking about trees. And in particular, look at walking a tree, or visiting the elements of a tree, or traversing the elements of a tree. So often we want to go through the nodes of a tree in a particular order.
We talked earlier, when we were looking at the syntax tree of an expression, how we could evaluate the expression by working our way up from the leaves. So that would be one way of walking through a tree in a particular order so we could evaluate. Another example might be printing the nodes of a tree. If we had a binary search tree, we might want to get the elements of a tree in sorted order.
Depth-first: So there, we completely traverse one sub-tree before we go on to a sibling sub-tree.
Breadth-first: We traverse all the nodes at one level before we go to the next level. So in that case, we would traverse all of our siblings before we visited any of the children of any of the siblings
Today, I am going to discuss DFS... DFS can be traversed in different ways (In Order, Pre Order and Post Order)
if tree = nil:
return
InOrderTraversal(tree.left)
Print(tree.key)
InOrderTraversal(tree.right)
So, we're going to have a recursive implementation 1-where if we have a nil tree, we do nothing 2-otherwise,
So let's look at an example of this.
We've got our binary search tree. And we're going to look at how these nodes get printed out if we do an in-order traversal. So to begin with:
So we see we get the elements out in sorted order. And again, we do the left child. And then the node and then the right child. And by our definition of a binary search tree, that gives them to us in order because we know all the elements in the left child are in fact less than or equal to the node itself.
Here is the code in C++
void printInOrderTraversal(struct node* node)
{
if (node == NULL)
return;
//Check the left, recur on left child
printInOrderTraversal(node->left);
//then print the data of node
cout<<node->data<<" ";
//then check the right, recur on right child
printInOrderTraversal(node->right);
}
The next depth-first traversal is a pre-order traversal. But we have to know why we use pre-order and post-order traversal?
if tree = nil:
return
Print(tree.key)
PreOrderTraversal(tree.left)
PreOrderTraversal(tree.right)
So let's look at an example of this.
So here the pre-order traversal says, we're going to go ahead first if it's nil we return. We print the key first, that is, we visit the node itself and then its children.
Here is the code in C++
void printPreorderTraversal(struct node* node)
{
if (node == NULL)
return;
//print data of node
cout<<node->data<<" ";
//then check the left, recur on left sutree
printPreorderTraversal(node->left);
//then check the right, recur on right subtree
printPreorderTraversal(node->right);
}
A post-order traversal is like a pre-order traversal expect instead of printing the node itself first, which is a pre, we print it last, which is the post. So all we've really done is move where this print statement is.
if tree = nil:
return
PostOrderTraversal(tree.left)
PostOrderTraversal(tree.right)
Print(tree.key)
So let's look at an example of this.
And here then, what's the last of these notes that's going to be printed?? Well it's actually going to be Les, because we're not going to be able to print Les until we've finished
One thing to note about the recursive traversal is we do have sort of under the covers, a stack that's being used. Because in a recursive call, every time we make a call back to a procedure, we are invoking another frame on the stack. So we are saving implicitly our information of where we are on the stack.
Here is the code in C++
void printPostorderTraversal(struct node* node)
{
if (node == NULL)
return;
// first check the left, recur on left sutree
printPostorderTraversal(node->left);
// then check the right,recur on right subtree
printPostorderTraversal(node->right);
//print data of node
cout<<node->data<<" ";
}
##Here is full example: {% gist https://gist.github.com/mohamedkhaledyousef/f743b78434ce1043865ca70932dfd7a0 %}
###Finally … [Here] (https://github.com/mohamedkhaledyousef/Crash-courses/blob/master/Trees/DFS.cpp) is the repo, You can find all source code in c++.