0% found this document useful (0 votes)
10 views

Tree Traversal

The document discusses tree traversal algorithms including preorder, inorder, and postorder traversal. It provides pseudocode for algorithms to perform each type of traversal on a binary tree represented using linked lists. It also includes C code examples to create a sample binary tree and perform traversals on it by calling the traversal functions. The document covers representing binary trees in memory using linked lists and arrays, and algorithms to traverse trees using stacks.

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Tree Traversal

The document discusses tree traversal algorithms including preorder, inorder, and postorder traversal. It provides pseudocode for algorithms to perform each type of traversal on a binary tree represented using linked lists. It also includes C code examples to create a sample binary tree and perform traversals on it by calling the traversal functions. The document covers representing binary trees in memory using linked lists and arrays, and algorithms to traverse trees using stacks.

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Lovely Professional University, Punjab

Data Structures
Lecture: Tree Traversal
Contents
• Introduction
• Representing Binary Tree in Memory
• Linked Representation
• Sequential Representation
• Traversing Binary Tree
• Preorder
• Inorder
• Postorder
• Traversal algorithms using Stacks
• Review Questions
Introduction
• Trees are non-linear data structures.

• Trees can be represented in memory using linked list


(linked representation) and arrays (sequential
representation).

• One should have direct access to the root R of T and,


given a node N of T, one should have direct access to
the children of N.
Linked Representation of Binary Tree

• A pointer variable ROOT and three parallel arrays


(INFO, LEFT and RIGHT) are used.
• Each node N of Tree T corresponds to a location K
such that:
(1) INFO[K] contains the data at the node N.
(2) LEFT [K] contains the location of the left child of node
N.
(3) RIGHT[K] contains the location of the right child of
node N.
• If any subtree is empty then corresponding pointer
will contain the NULL value.

• If Tree is empty then Root will contain NULL.

• An entire record may be stored at the node N.


Linked Representation

INFO LEFT
RIGHT 2 ROOT0
A 6 4
E 0 0
10 C 0 9
AVAIL 8
B 7 3
D 0 0
1
F 0 0
5
Sequential Representation of Binary Tree

• Efficient for the tree T that is complete or nearly


complete.

• Use of only a single linear array TREE such that:


(a) The Root of T is stored in TREE [1].
(b) If a node N occupies Tree [K], then its left child is stored
in TREE [2*K] and right child is stored in TREE [2*K + 1].
Inorder Traversal of Binary Tree
INORDER (INFO, LEFT, RIGHT, ROOT )
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat while PTR != NULL:
(a) Set TOP = TOP+1. and STACK[TOP] = PTR.
(b) Set PTR = LEFT[PTR].
3. Set PTR = STACK [TOP] and TOP = TOP – 1.
4. Repeat Step 5 to 7 while PTR != NULL:
5. Apply PROCESS to INFO[PTR].
6. If RIGHT[PTR] != NULL, then:
(a) Set PTR = RIGHT[PTR].
(b) Go to Step 2.
7. Set PTR = STACK[TOP] and TOP = TOP-1.
8. Exit.
Preorder Traversal of Binary Tree
PREORDER (INFO, LEFT, RIGHT, ROOT )
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat step 3 to 5 while PTR != NULL:
3. Apply PROCESS to INFO[PTR].
4. If RIGHT[PTR] != NULL, then:
Set TOP = TOP+1. and
STACK[TOP] = RIGHT[PTR].
5. If LEFT[PTR] != NULL, then:
Set PTR = LEFT[PTR].
Else
Set PTR = STACK [TOP] and TOP = TOP –
1.
6. Exit.
Postorder Traversal of Binary Tree
POSTORDER (INFO, LEFT, RIGHT, ROOT )
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat step 3 to 5 while PTR != NULL:
3. Set TOP = TOP+1. and STACK[TOP] = PTR.
4. If RIGHT[PTR] != NULL, then:
Set TOP = TOP+1. and STACK[TOP] = -RIGHT[PTR].
5. Set PTR = LEFT[PTR].
6. Set PTR = STACK [TOP] and TOP = TOP – 1.
7. Repeat while PTR >0:
(a) Apply PROCESS to INFO[PTR].
(b) Set PTR = STACK[TOP] and TOP = TOP-1.
8. If PTR < 0, then:
(a) Set PTR = - PTR
(b) Go to Step 2.
9. Exit.
#include <stdio.h> // Create a new Node
#include <stdlib.h> struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
struct node { newNode->left = NULL;
int item; newNode->right = NULL;
struct node* left;
struct node* right; return newNode;
}; }
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
// Inorder traversal root->left = createNode(value);
void inorderTraversal(struct node* root) { return root->left;
if (root == NULL) return; }
inorderTraversal(root->left); // Insert on the right of the node
printf("%d ->", root->item); struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
inorderTraversal(root->right);
return root->right;
} }
int main() {
// preorderTraversal traversal struct node* root = createNode(1);
void preorderTraversal(struct node* root) { insertLeft(root, 12);
if (root == NULL) return; insertRight(root, 9);
printf("%d ->", root->item);
insertLeft(root->left, 5);
preorderTraversal(root->left); insertRight(root->left, 6);
preorderTraversal(root->right);
} printf("Inorder traversal \n");
// postorderTraversal traversal inorderTraversal(root);
void postorderTraversal(struct node* root) {
printf("\nPreorder traversal \n");
if (root == NULL) return;
preorderTraversal(root);
postorderTraversal(root->left);
postorderTraversal(root->right); printf("\nPostorder traversal \n");
printf("%d ->", root->item); postorderTraversal(root);
} }
Questions

You might also like