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

Binary Tree and Its Traversal

Tree data structures can be linear or non-linear. Linear structures like arrays and linked lists have elements that are totally ordered, while non-linear structures like trees and graphs have elements that are not. A tree is a set of nodes where there is one root node and the remaining nodes are partitioned into disjoint subtrees, with each subtree also being a tree. Common tree operations include traversing the tree using preorder, inorder, or postorder traversal methods.

Uploaded by

sneha
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)
72 views

Binary Tree and Its Traversal

Tree data structures can be linear or non-linear. Linear structures like arrays and linked lists have elements that are totally ordered, while non-linear structures like trees and graphs have elements that are not. A tree is a set of nodes where there is one root node and the remaining nodes are partitioned into disjoint subtrees, with each subtree also being a tree. Common tree operations include traversing the tree using preorder, inorder, or postorder traversal methods.

Uploaded by

sneha
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/ 67

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

Root is the node which has no parent.


I Level 3
Siblings are nodes which has common parent.
Leaves are node which has no child node.
Every node except Root node has parent.
Tree Terminology

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

Leaves
F G H Level 2

A is Root Node
I Level 3
A is Parent of B, C, D and E
F and G are Child of D
F, and G are Siblings
B, C, F, G and I are leaves
Tree Terminology

Depth of a node is equal to its level.

Height of Tree is equal to the total number of levels.

The Degree of node is equal to its Childs.

 External Node: a node which has no child.

Internal Node: a node which has atleast one child.


Tree Terminology

A Level 0

Level 1
C D E
B

Depth of H (level) is 2 F G H Level 2

Height of Tree is 3
The Degree of node D is 2
I Level 3
B, C, F, G and I are External
A , D, E, H 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

Ordered Binary Tree


Ex: of a strictly binary tree
A

B C

D E

F G
A COMPLETE BINARY TREE OF DEPTH 3

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
Preorder Traversal

Steps:

• Visit the root

• Traverse the left subtree in preorder

• 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:

• Traverse the left subtree in inorder

• Visit the root

• 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:

• Traverse the left subtree in postorder

• Traverse the right subtree in postorder

• 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

You might also like