0% found this document useful (0 votes)
10 views38 pages

CSE220 L11 Tree PNP

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)
10 views38 pages

CSE220 L11 Tree PNP

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/ 38

Data Structures

Lecture 11
Tree
Prantik Paul [PNP]
Lecturer
Department of Computer Science and Engineering
BRAC University 1
Trees

2
Why Trees?

Sorting New Elements

Folder/File System Structure

Computer Network Algorithms

3
Trees - Root/Leaf/Non-Leaf

4
Trees - Parent/Child/Siblings

5
Trees - Edge/Path
n nodes → n-1 edges

6
Trees - Degree

7
Trees - Depth/Height/Level

8
Trees - Depth/Height/Level

Depth of A? Height of Depth != Height


A?
Depth = Level 9
Trees - Subtree

10
Trees - Characteristics

11
Trees - Build a Tree

Dynamic
Sequential
Representation
Representation
withwith
Linked
Array
List 12
Binary Trees

13
Binary Trees

14
Binary Tree - Full/Strict Binary Tree

No of leaf nodes = no of internal nodes + 1 15


Binary Tree - Complete Binary Tree

All levels filled starting from LEFT 16


Binary Tree - Perfect Binary Tree

All Internal Nodes have 2 child


All Leaf Nodes at same Level 17
Binary Tree - Balanced Binary Tree

| height(left) - height(right) | <= 1 18


Binary Trees - Characteristics

The maximum number of nodes at level i is: 2i 19


Binary Trees - Characteristics

The maximum number of nodes possible in a binary tree


of height ‘h’ is: 2h+1 – 1 20
Binary Trees - Characteristics

Number of internal nodes : n


Number of external nodes : n+1 21
Binary Trees - Characteristics

Number of internal nodes : n


Number of internal edges : n-1 22
Binary Trees - Characteristics

Number of internal nodes : n


Number of external edges : n+1 23
Binary Trees - Characteristics

Number of internal nodes : n


Number of edges : 2n 24
Binary Trees - Traversal (Pre-order)

25
Binary Trees - Traversal (In-order)

26
Binary Trees - Traversal (Post-order)

27
Binary Trees - Array Representation

If the height of the binary tree if h, An array of maximum


2h+1 length is required

The root is placed at index 1

Any node that is placed at index i, will have its left child
placed at 2i and its right child at 2i+1
28
Binary Trees - Array Representation

29
Binary Trees - Tree Node

30
Binary Trees - Preorder Traversal

31
Binary Trees - Inorder Traversal

Left → print(root.elem) → Right

Do yourself
32
Binary Trees - Postorder Traversal

Left → Right → print(root.elem)

Do yourself
33
Binary Trees - Count Nodes of Tree

34
Binary Trees - Find Level of a Node

35
Binary Trees - Find Height of a Node

If node is None
return -1
else
return 1 + max(left_subtree_height, right_subtree_height)

Do yourself
36
Binary Trees - Build Tree From Array

37
Binary Trees - Build Array From Tree

38

You might also like