xxxxxxxxxx
def postOrder(root):
if root:
postOrder(root.left)
postOrder(root.right)
print(root, end = " ")
xxxxxxxxxx
<> Preorder Root -> Left -> Right
<> Inorder Left -> Root -> Right
<> Postorder Left -> Right -> Root
xxxxxxxxxx
A tree is a graph whose degree of node== # of it's children & is acyclic
Binary tree is a tree with each node having atmost 2 children.
2 ways to traverse each node once:
BFS - level wise
DFS - BY RECURSIVELY TRAVERSING ROOT,LEFT SUBTREE (L) & RIGHT SUBTREE (R)
NOTE: THERE ARE 3! WAYS OF DOING A DFS, BASED ON ORDER OF Root,L,R.
but only 3 are useful :
Root,L,R- preorder traversal
L,Root,R- inorder traversal
L,R,Root- postorder traversal
xxxxxxxxxx
# Assuming valid Binary Tree Node
def post_order(node: BinaryTreeNode):
if node.left:
post_order(node.left)
if node.right:
post_order(node.right)
print(node.val)
xxxxxxxxxx
//*******this is a C program for implementation and searching in A BT*******
#include<stdlib.h>
#include <stdio.h>
struct BinaryTree{
int data;
struct BinaryTree*right;
struct BinaryTree*left;
};
struct BinaryTree*createnode(int val){
struct BinaryTree*root=(struct BinaryTree*)malloc(sizeof(struct BinaryTree));
root->data=val;
root->left=NULL;
root->right=NULL;
}
void inorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else {inorder(root->left);
printf("%d->",root->data);
inorder(root->right);
}
}
void preorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else {
printf("%d->",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else{
postorder(root->left);
postorder(root->right);
printf("%d->",root->data);
}
}
int main()
{
printf("Lets grow the tree\n");
struct BinaryTree*root=createnode(1);
root->left=createnode(2);
root->right=createnode(3);
root->left->left=createnode(4);
root->left->right=createnode(5);
printf("tree has grown up\n");
printf("Inorder traversal ");
inorder(root);printf("NULL");
printf("\npreorder traversal ");
preorder(root);printf("NULL");
printf("\nPostorder traversal");
postorder(root);printf("NULL");
return 0 ;
}
xxxxxxxxxx
class Node:
def __init__(self,data):
self.data = data
self.parent = None
self.left = None
self.right = None
def __repr__(self):
return repr(self.data)
def add_left(self,node):
self.left = node
if node is not None:
node.parent = self
def add_right(self,node):
self.right = node
if node is not None:
node.parent = self
'''
Example:
_2_
/ \
7 9
/ \ \
1 6 8
/ \ / \
5 10 3 4
'''
def create_tree():
two = Node(2)
seven = Node(7)
nine = Node(9)
two.add_left(seven)
two.add_right(nine)
one = Node(1)
six = Node(6)
seven.add_left(one)
seven.add_right(six)
five = Node(5)
ten = Node(10)
six.add_left(five)
six.add_right(ten)
eight = Node(8)
three = Node(3)
four = Node(4)
nine.add_right(eight)
eight.add_left(three)
eight.add_right(four)
# now return the root node
return two
def pre_order(node):
print(node)
if node.left:
pre_order(node.left)
if node.right:
pre_order(node.right)
def in_order(node):
if node.left:
in_order(node.left)
print(node)
if node.right:
in_order(node.right)
def post_order(node):
if node.left:
post_order(node.left)
if node.right:
post_order(node.right)
print(node)
if __name__ == "__main__":
root = create_tree()
print("\nPre-order traversal:")
pre_order(root)
print("\nIn-order traversal:")
in_order(root)
print("\nPost-order traversal:")
post_order(root)
xxxxxxxxxx
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
dfs(root);
return list;
}
public void dfs(TreeNode root)
{
if(root!=null)
{
dfs(root.left);
dfs(root.right);
list.add(root.val);
}
}
}
xxxxxxxxxx
1. Right child of 1 exists.
Push 3 to stack. Push 1 to stack. Move to left child.
Stack: 3, 1
2. Right child of 2 exists.
Push 5 to stack. Push 2 to stack. Move to left child.
Stack: 3, 1, 5, 2
3. Right child of 4 doesn't exist. '
Push 4 to stack. Move to left child.
Stack: 3, 1, 5, 2, 4
4. Current node is NULL.
Pop 4 from stack. Right child of 4 doesn't exist.
Print 4. Set current node to NULL.
Stack: 3, 1, 5, 2
5. Current node is NULL.
Pop 2 from stack. Since right child of 2 equals stack top element,
pop 5 from stack. Now push 2 to stack.
Move current node to right child of 2 i.e. 5
Stack: 3, 1, 2
6. Right child of 5 doesn't exist. Push 5 to stack. Move to left child.
Stack: 3, 1, 2, 5
7. Current node is NULL. Pop 5 from stack. Right child of 5 doesn't exist.
Print 5. Set current node to NULL.
Stack: 3, 1, 2
8. Current node is NULL. Pop 2 from stack.
Right child of 2 is not equal to stack top element.
Print 2. Set current node to NULL.
Stack: 3, 1
9. Current node is NULL. Pop 1 from stack.
Since right child of 1 equals stack top element, pop 3 from stack.
Now push 1 to stack. Move current node to right child of 1 i.e. 3
Stack: 1
10. Repeat the same as above steps and Print 6, 7 and 3.
Pop 1 and Print 1.
xxxxxxxxxx
Inorder Traversal using Stack:
As we already know, recursion can also be implemented using stack. Here also we can use a stack to perform inorder traversal of a Binary Tree. Below is the algorithm for traversing a binary tree using stack.
Create an empty stack (say S).
Initialize the current node as root.
Push the current node to S and set current = current->left until current is NULL
If current is NULL and the stack is not empty then:
Pop the top item from the stack.
Print the popped item and set current = popped_item->right
Go to step 3.
xxxxxxxxxx
Following is a simple stack based iterative process to print Preorder traversal.
Create an empty stack nodeStack and push root node to stack.
Do the following while nodeStack is not empty.
Pop an item from the stack and print it.
Push right child of a popped item to stack
Push left child of a popped item to stack
The right child is pushed before the left child to make sure that the left subtree is processed first.