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
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
xxxxxxxxxx
def postOrder(root):
if root:
postOrder(root.left)
postOrder(root.right)
print(root, end = " ")
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node * left, * right;
};
vector < int > preOrderTrav(node * curr) {
vector < int > preOrder;
if (curr == NULL)
return preOrder;
stack < node * > s;
s.push(curr);
while (!s.empty()) {
node * topNode = s.top();
preOrder.push_back(topNode -> data);
s.pop();
if (topNode -> right != NULL)
s.push(topNode -> right);
if (topNode -> left != NULL)
s.push(topNode -> left);
}
return preOrder;
}
struct node * newNode(int data) {
struct node * node = (struct node * ) malloc(sizeof(struct node));
node -> data = data;
node -> left = NULL;
node -> right = NULL;
return (node);
}
int main() {
struct node * root = newNode(1);
root -> left = newNode(2);
root -> right = newNode(3);
root -> left -> left = newNode(4);
root -> left -> right = newNode(5);
root -> left -> right -> left = newNode(8);
root -> right -> left = newNode(6);
root -> right -> right = newNode(7);
root -> right -> right -> left = newNode(9);
root -> right -> right -> right = newNode(10);
vector < int > preOrder;
preOrder = preOrderTrav(root);
cout << "The preOrder Traversal is : ";
for (int i = 0; i < preOrder.size(); i++) {
cout << preOrder[i] << " ";
}
return 0;
}
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.