0% found this document useful (0 votes)
9 views41 pages

DS Lecture 8

The document is a lecture on trees in data structures, focusing on their introduction, types, and operations. It explains the hierarchical nature of trees, the definitions of nodes, and various types of binary trees and their traversal methods. Additionally, it covers the applications of binary search trees in organizing and managing data efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views41 pages

DS Lecture 8

The document is a lecture on trees in data structures, focusing on their introduction, types, and operations. It explains the hierarchical nature of trees, the definitions of nodes, and various types of binary trees and their traversal methods. Additionally, it covers the applications of binary search trees in organizing and managing data efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

2nd level _Cyber Security and Networks

Data Structures and Algorithms

Lecture 8

Dr. Taghreed Abdullah


1
‫‪Content‬‬

‫مقدمة األشجار‬
‫أنواع الشجرة الثنائية‬

‫‪ ‬عمليات الشجرة الثنائية‬

‫‪Dr. Taghreed Abdullah‬‬ ‫‪2‬‬


Trees- introduction

Dr. Taghreed Abdullah 3


Trees- introduction

Linear Data Structure Non Linear Data Structure

Stack Tree

Queue

Linked List

Dr. Taghreed Abdullah 4


Trees- introduction

How should we decide


which data structure to
 What needs to stored?
use?  Cost of operations
 Memory Usage
 Ease of implementation

Dr. Taghreed Abdullah 5


Trees- introduction

What do we use Tree


for?

Manager
(Waleed)

Teacher Teacher
(Islam) (Amani)

Ahamed Ali Fatema Ahlam

Dr. Taghreed Abdullah 6


Trees- introduction

Node Root Root


Edge

Branch

Branch (sub-tree)
Leaves
Leaves
Dr. Taghreed Abdullah 7
Trees- introduction

Tree:
Root  Collection of entities called nodes linked together
to simulate hierarchy
 Tree is non linear data structure
 Tree is hierarchal data structure
 Topmost node is called Root of the tree
 Each node contains some data of any type and
contain link to another nodes that can be called
its children

A tree is a hierarchal data structure that organizes data


elements, called nodes, by connecting them with links, called
Dr. Taghreed Abdullah
edges 8
Trees- introduction

Root

1 Parent: node has children


Child: node has parent

2 3 Sibling: nodes have same parent


Uncle: node is sibling with the parent
of specific node
4 5 6 7 8
Cousin: nodes have same grand parent

9 10 11

Dr. Taghreed Abdullah 9


Trees- introduction

Root
 Node types:
1  Node Root
 a node has no parent
 1 is the root node
2 3
 Leaf Node:
 is a node has no child
4  4, 6, 8, 9, 10, 11 are
5 6 7 8
leaf nodes
 Interior Node:
9 10 11  A node which is neither a root nor a leaf
 2, 3, 5, 7 are interior nodes

Dr. Taghreed Abdullah 10


Trees- introduction

Root

1
If we can go from node A to node B
 A is called ancestor of B
2 3
 B is called descendent of A

4 5 6 7 8 Example
1,2,5 are ancestor of 10
9 10 11 10 is descendent of 1,2,5

Dr. Taghreed Abdullah 11


Trees- introduction

Root

1 What are common ancestor of 4, 9?


1,2 are the common ancestor of 4,9
2 3
6,7 have same grand parent so

4 8
they called cousin
5 6 7

9 10 11

Dr. Taghreed Abdullah 12


Trees- introduction

Root
1 3 is uncle of 6

2 3

4 5 6 7 8

9 10 11

Dr. Taghreed Abdullah 13


Trees- introduction
Root
1 Tree is Recursive data structure
Sub-tree Sub-tree

2 3

4 5 6 7 8

9 10 11

Dr. Taghreed Abdullah 14


Trees- introduction
Root
1 Tree is Recursive data structure
Sub-tree Sub-tree

2 3

4 5 6 7 8

9 10 11

Dr. Taghreed Abdullah 15


Trees- introduction

Root
Trees
1
 N nodes
 N-1 edges
2 3

4 5 6 7 8

9 10 11

Dr. Taghreed Abdullah 16


Trees- introduction

Root Height of Node


1
Height of node x =
2 3 No. of edges in longest path from
x to a leaf
Examples:
4 5 6 7 8
Height of 11 = 2
Height of 4 = 1
9 10 11

Dr. Taghreed Abdullah 17


Trees- introduction

Root
Depth of Node
1

Depth of node x=
2 3 Length of path from root to x
Or
4 5 6 7 8 No. of edges in path from root to x
Examples:
9 10 11 Depth of 2 = 1
Depth of 8 = 2
Depth of Root node=3
Dr. Taghreed Abdullah 18
Trees- introduction

Root
Height or Depth of Tree
1
no. of edges in longest path from
root to a leaf
2 3

Height of Root node


4 5 6 7 8
Height of empty tree
Height of tree with one node
9 10 11

Height of tree = 3
Dr. Taghreed Abdullah 19
Trees- introduction

 Root
Root
 Children
1
 Parent
 Sibling
2 3
 Cousin
 Uncle
4 5 6 7 8
 Leaf
 Ancestor
9 10 11
 Descendent
 Height
 Depth
Dr. Taghreed Abdullah 20
Trees- introduction

Types of Tree
 General tree
 any number of nodes
 any number of children per
node

 Binary Tree
 Only one Root
 Maximum 2 children per node
 Each node can have no child or
two children
Dr. Taghreed Abdullah 21
Trees- Binary Tree

Binary Tree Traversal


 Pre Order
 Root left Right
 In Order
 left Root Righ
 Post Order
 left Right Root

Dr. Taghreed Abdullah 22


Trees- Binary Tree

Binary Tree Traversal


a
 Pre Order
 Root left Right
b c

d e f a b d g h e i c f j

g h i j

Dr. Taghreed Abdullah 23


Trees- Binary Tree

Binary Tree Traversal


a
 In Order
 left Root Right
b c

d e f g d h b e i a f j c

g h i j

Dr. Taghreed Abdullah 24


Trees- Binary Tree

Binary Tree Traversal


a
 Post Order
 left Right Root
b c

d e f g h d i e b j f c a

g h i j

Dr. Taghreed Abdullah 25


Trees- Binary Tree (Types)

Full binary tree Complete binary tree Perfect binary tree


All nodes of a binary tree All the levels are completely It is full binary tree all the
have either 0 or 2 children filled except possibly the last nodes contains exactly two

level children other than the leaf


nodes
Dr. Taghreed Abdullah 26
Trees- Binary Trees

Max No. of nodes at level i =


𝑳𝟎 =1

𝑳𝟏 =2

𝑳𝟐 =4

𝑳𝟑 =8

Dr. Taghreed Abdullah 27


Trees- Binary Trees

Perfect Binary tree


Max No. of nodes at level i =
𝑳𝟎 =1 Max No. of nodes in a tree with
height h=
𝑳𝟏 =2

𝑳𝟐 =4

𝑳𝟑 =8

Dr. Taghreed Abdullah 28


Trees- Binary Trees

Perfect Binary tree


Max No. of nodes at level i =
𝑳𝟎 =1 Max No. of nodes in a tree with
height h=
𝑳𝟏 =2

𝑳𝟐 =4

𝑳𝟑 =8

Max-height

Dr. Taghreed Abdullah 29


Trees- Binary Trees

Dr. Taghreed Abdullah 30


Trees- Binary Trees

Perfect Binary tree

Difference between height of left


and right sub-tree for every
node is not more than k (mostly
1)

Dr. Taghreed Abdullah 31


Trees- Binary Trees

Address of Address of
the left child the right child

We can implement binary


tree using:
1) Dynamically created
nodes
struct node {
int data;
node* left
node* right
};

Dr. Taghreed Abdullah 32


Trees- Binary Trees

20
2) Array
For node at index

12 25 Left-child-index
Right-child-index

2 14 0 1 2 3 4 5 6
18 30
20 12 25 2 14 18 30

Dr. Taghreed Abdullah 33


Trees- Binary Trees

Binary Search Tee (BST)

Dr. Taghreed Abdullah 34


Trees- Binary Trees

Binary Search Operations

Dr. Taghreed Abdullah 35


Trees- Binary Trees

Binary Search Tee (BST)

Dr. Taghreed Abdullah 36


Trees- Binary Trees

Searching

Dr. Taghreed Abdullah 37


Trees- Binary Trees

Insertion BST

Dr. Taghreed Abdullah 38


Trees- Binary Trees

deletion BST

Dr. Taghreed Abdullah 39


Trees- Binary Trees

Applications

1)Storing naturally hierarchical data e.g., file system

2)Organize data

3)For quick search, insertion, deletion e.g., binary search tree

4)Network routing algorithm

Dr. Taghreed Abdullah 40


Thank you

Dr. Taghreed Abdullah 41

You might also like