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

Tree

The document provides an overview of linear and non-linear data structures, focusing on trees and their characteristics. It explains tree terminology, types of binary trees, and various tree traversal methods including preorder, inorder, postorder, and level order. Additionally, it discusses operations on binary trees and the properties of complete binary trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Tree

The document provides an overview of linear and non-linear data structures, focusing on trees and their characteristics. It explains tree terminology, types of binary trees, and various tree traversal methods including preorder, inorder, postorder, and level order. Additionally, it discusses operations on binary trees and the properties of complete binary trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 102

Tree

Linear and Non Linear Data Structure

• Arrays, Linked lists, Stacks, Queues are linear


structure.
 Why linear?
• The elements are totally ordered.
• For any pair of elements, one precedes the other.

• We can have non-linear structure


Tree, Graph are non linear data strucutre
• Elements are not totally ordered.
Tree Data structure

 A tree is a finite set of one or more nodes such that:


 There is a specially designated node called the root.
 The remaining nodes are partitioned into n>=0 disjoint sets
T1, ..., Tn, where each of these sets is a tree.
 We call T1, ..., Tn the subtrees of the root.
Tree representation

C D E F

H I J K L M

P Q
Tree representation

A Root

C D E F

H I J K L M

Subtree with Q
P
single node Subtree
with root F
Subtree with
root D

Subtree with root E


Tree Terminology

A Level 0
Root
Parent
Child Level 1
C D E
Siblings B

Leaves
F G H Level 2

I Level 3
Tree Terminology

A Level 0
Root
Parent
Child Level 1
C D E
Siblings B

Leaves
F G H Level 2

A is ________
I Level 3
A is the Parent of __________
_______ are Child of D
F and G are_________
_______________ are leaves
Tree Terminology

 The depth of a node is the number of edges from the root to the
node.
The height of a node is the number of edges from the node to
the deepest leaf.

The height of a tree is the height of the root node.

The Degree of a node is equal to no. of its Child nodes.

 External Node: a node that has no child.

Internal Node: a node that has at least one child.


Tree Terminology

A Level 0

Level 1
C D E
B

F G H Level 2
Depth of H (or level of H) is ______
Height of Tree is _______
I Level 3
The Degree of node D is ____
_________ are External
________ are internal node of Tree
Binary Trees
Binary Tree

Binary Tree:
 Ordered tree with all nodes having at most two children
Some examples
A

B C

D E F

G H I

Binary tree
Structures which are not binary trees
A

B C

D E F

G I
Various binary trees

• Ordered binary Tree:


 Is a tree in which the children of each node are ordered.

• Strictly Binary Tree:


If every non leaf node in a binary tree has non empty left and
right sub trees then such tree is termed as a strictly binary tree.

• Complete Binary Tree:


 A complete binary tree of height h is the strict binary tree
all of whose leaves are at level h.
1
A

2 B 3 C

4 D 5 E 6 F 7 G

8 H 9 I J 10

Example: Ordered Binary Tree


Ex: A ________ binary tree
A

B C

D E

F G
A ________ BINARY TREE OF DEPTH ____

B C

D E G F

H I J K L M N O
Complete Binary Tree

• Level i has 2i nodes


• in a tree of height h
 Leaves are at level h
 No of leaves is 2h
 No of internal nodes is 2h -1
 Total nodes N= 2h + 2h -1
N = 2. 2h -1= 2h+1 -1
 N=2h+1 -1 --- log2N=h+1-log21
 log2N =h+1-h= log2N-1
 h= O(log2N)
Operations on Binary Trees

• Finding duplicates
• Traversing a binary tree
• Searching
Binary Tree Traversals
Tree Traversal

• Many binary tree operations are done by


performing a traversal of the binary tree.

• In a traversal, each element of the binary tree is


visited exactly once.
Three tree traversal methods

• Preorder traversal

• Inorder traversal

• Postorder traversal

• Level Order traversal


Preorder Traversal

Steps:

i. Visit the root

ii. Traverse the left subtree in preorder

iii. Traverse the right subtree in preorder


Traversing a Tree Preorder

B C

D E F G

H I
Traversing a Tree Preorder

B C

D E F G

H I

Result: A
Traversing a Tree Preorder

B C

D E F G

H I

Result: AB
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABD
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABDE
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABDEH
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABDEHC
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABDEHCF
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABDEHCFG
Traversing a Tree Preorder

B C

D E F G

H I

Result: ABDEHCFGI
Inorder Traversal (symmetric order)

Steps for a nonempty binary tree:

i. Traverse the left subtree in inorder

ii. Visit the root

iii. Traverse the right subtree in inorder


Traversing a Tree Inorder

B C

D E F G

H I
Traversing a Tree Inorder

B C

D E F G

H I
Traversing a Tree Inorder

B C

D E F G

H I
Traversing a Tree Inorder

B C

D E F G

H I

Result: D
Traversing a Tree Inorder

B C

D E F G

H I

Result: DB
Traversing a Tree Inorder

B C

D E F G

H I

Result: DB
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBH
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHE
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHEA
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHEA
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHEAF
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHEAFC
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHEAFCG
Traversing a Tree Inorder

B C

D E F G

H I

Result: DBHEAFCGI
Postorder Traversal

Steps:

i. Traverse the left subtree in postorder

ii. Traverse the right subtree in postorder

iii. Visit the root


Traversing a Tree Postorder

B C

D E F G

H I
Traversing a Tree Postorder

B C

D E F G

H I

Result:
Traversing a Tree Postorder

B C

D E F G

H I

Result:
Traversing a Tree Postorder

B C

D E F G

H I

Result: D
Traversing a Tree Postorder

B C

D E F G

H I

Result: D
Traversing a Tree Postorder

B C

D E F G

H I

Result: DH
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHE
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEB
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEB
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEBF
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEBF
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEBFI
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEBFIG
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEBFIGC
Traversing a Tree Postorder

B C

D E F G

H I

Result: DHEBFIGCA
Tree Traversal – Example 1
A

B C

D E F

G H I

Preorder: ABDGCEHIF
Inorder: DGBAHEICF
Postorder:
GDBHIEFCA
Tree Traversal – Example 2
A

Preorder: ABCEIFJDGHKL
Inorder: EICFJBGDKHLA
Postorder: IEJFCGKLHDBA
B

C D

G H
E F

K L
I J
Preorder, Postorder and Inorder
Level Order Traversal

i. Start at the root


ii. Visit the nodes at each level, from left to
right

• Is there a recursive algorithm for a level


order traversal?
Level Order Traversal
i. Start at the root
ii. Visit the nodes at each level, from left to right

B C

D E F G

queue
H I
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
A
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I

A
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
BC
A
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
C
A B
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
CDE
A B
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
DEFG
A B C
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
EFGH
A B C D
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
FGH
A B C D E
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
GHI
A B C D EF
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
HI
A B C D E F G
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I
I
A B C D E F GH
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right

B C

D E F G

H I

A B C D E F G H I
Level Order Traversal

B C

D E F G

H I

Nodes will be visited in the order ABCDEFGHI


Level order Traversal
Non-Recursive Pre order

• Since we are not using recursion, we will use


the Stack to store the traversal
• We need to remember that preorder traversal
is:
first traverse the root node then left node
followed by the right node.
Non Recursive Pre order

1) Create an empty stack nodeStack and push root node to stack.


2) Do following while nodeStack is not empty.
….a) Pop an item from stack and print it.
….b) Push right child of popped item to stack
….c) Push left child of popped item to stack

• The right child is pushed before the left child to make sure
that left subtree is processed first.
Non-Recursive In-order

1) Create an empty stack S.


2) Initialize current node as root
3) Push the current node to S and set current = current->left until
current is NULL
4) If current is NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set current = popped_item->right
c) Go to step 3.
5) If current is NULL and stack is empty then we are done.
Non Recursive Post order
1 Create an empty stack
2.1 Do the following while root is not NULL
a) Push root's right child and then root to stack.
b) Set root as root's left child.
2.2 Pop an item from stack and set it as root.
a) If the popped item has a right child and the right child is at top of
stack, then remove the right child from stack, push the root back and
set root as root's right child.
b) Else print root's data and set root as NULL.
2.3 Repeat steps 2.1 and 2.2 while stack is not empty.
Creating Binary tree from Pre-order/Post-
order/In-order
Pre order and In order

• Inorder sequence: D B E A F C
• Preorder sequence: A B D E C F
Pre order and In order

• Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
Pre order and In order

• Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
• Pre order: 1, 2, 4, 3, 5, 7, 8,6
• In order: 4, 2, 1, 7, 5, 8, 3, 6
• Pre order: 1, 2, 4, 3, 5, 7, 8,6
• In order: 4, 2, 1, 7, 5, 8, 3, 6
• Pre order: 1, 2, 4, 3, 5, 7, 8,6
• In order: 4, 2, 1, 7, 5, 8, 3, 6
Post order and In order

inOrder = { 4, 2, 5, 1, 6, 3, 7 };
postOrder = { 4, 5, 2, 6, 7, 3, 1 };
Level order and In order

• in[] = {4, 8, 10, 12, 14, 20, 22};


• level[] = {20, 8, 22, 4, 12, 10, 14};
Level order and In order

• in[] = {4, 8, 10, 12, 14, 20, 22};


• level[] = {20, 8, 22, 4, 12, 10, 14};
Create Tree using Pre-Order and Post Order

• It is not possible to construct a general


Binary Tree from preorder and postorder
traversals. But if know that the Binary Tree
is Full, we can construct the tree without
ambiguity.
• This is so because we cannot separate the
left sub-tree and right sub-tree using the
pre-order or post-order traversal alone.
Create Tree using Pre-Order and Post Order

• A Full Binary Tree is a binary tree where


every node has either 0 or 2 children
Create Tree using Pre-Order and Post Order

• A Full Binary Tree is a binary tree where


every node has either 0 or 2 children
Create Tree using Pre-Order and Post Order
• pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7} and
• post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1}
• In pre[], the leftmost element is root of tree.
• Since the tree is full and array size is more than 1. The
value next to 1 in pre[], must be left child of root.
• So we know 1 is root and 2 is left child.
• How to find the all nodes in left subtree?
• We know 2 is root of all nodes in left subtree. All nodes
before 2 in post[] must be in left subtree.
• Now we know 1 is root, elements {8, 9, 4, 5, 2} are in left
subtree, and the elements {6, 7, 3} are in right subtree.
Create Tree using Pre-Order and Post Order
pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7} and
post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1}

You might also like