dsa prsentation binary tree and its variants.

MinahilNadeem11 29 views 29 slides Jul 10, 2024
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

Binary trees in the dsa subject.


Slide Content

Topic : General and Binary
Trees

Group Members
•MinahilNadeem
•SaadSaaed
•LaibaRustum
•MubbashirMalik
2

General Tree :
❖A general tree is a hierarchical structure where each node can have any
number of children (zero or more).
❖In a general tree, each node has in-degree(number of parent nodes) one
and maximum out-degree(number of child nodes) n.
3

General Tree :
Monday, February 1, 20XX Sample Footer Text
4

General Tree :
Monday, February 1, 20XX Sample Footer Text
5
Real-life example:
Consider an organizational
chart of a company. Each
employee can have several
reporting managers or
subordinates, creating a
complex hierarchy.

.
Monday, February 1, 20XX Sample Footer Text
6

Binary Tree:
❖A binary tree is the specialized version of the General tree. A binary tree is a
tree in which each node can have at most two nodes.
❖The topmost node of a binary tree is called root node and there are mainly
two subtrees one isleft-subtreeand another isright-subtree.
7

Binary Tree :
8

Binary Tree :
Real-life example:
Imagine a family tree. Each
person can have two parents,
forming a binary branching
structure.
9

10

Difference b/w General And Binary Tree :
11
Feature General Tree Binary Tree
Number of children per node Any number At most two (left and right)
Structure Complex, varying degree of branchingSimpler, consistent branching
Operations Standard traversal algorithms
Standard traversal algorithms + specific
binary tree traversals
Representations Node-based, parent-based Node-based, parent-based, array-based
Applications
Hierarchical relationships, file systems,
complex data
Data organization, retrieval, sorting,
searching
Special case None Binary search tree

Difference b/w General And Binary Tree :
Monday, February 1, 20XX
12

Representation of Binary Tree :
13
Array Representation :
In array representation of a binary tree, we use one-dimensional array (1-D Array) to
represent a binary tree.

Representation of Binary Tree :
Monday, February 1, 20XX Sample Footer Text
14
To represent a binary tree of depth'n'using array representation, we need one
dimensional array with a maximum size of2n + 1.

Representation of Binary Tree :
15
Linked List Representation of Binary Tree :
We use a double linked list to represent a binary tree. In a double linked list,
every node consists of three fields. First field for storing left child address,
second for storing actual data and third for storing right child address.

Representation of Binary Tree :
16

Traversal Techniques :
17
❖ForInorder,
you traverse from theleftsubtree to therootthen to therightsubtree.
❖ForPreorder,
you traverse from therootto theleftsubtree then to therightsubtree.
❖ForPost order,
you traverse from theleftsubtree to therightsubtree then to theroot.

18

Binary Search Tree :
19
•It is called a binary tree because each tree node has a maximum of two
children.
•It is called a search tree because it can be used to search for the presence of
a number inO(log(n))time.

Binary Search Tree :
Real-life example:
Imagine a dictionary. Each word has a
specific alphabetical order, creating a
binary search tree where searching for
a word involves comparisons with
neighboring words.
Monday, February 1, 20XX Sample Footer Text
20

Monday, February 1, 20XX Sample Footer Text
21

Binary Search Tree :
Algorithm:
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
Monday, February 1, 20XX Sample Footer Text
22

Binary Search Tree :
Monday, February 1, 20XX Sample Footer Text
234<8 so, transverse through the left child of 8

Binary Search Tree :
Monday, February 1, 20XX Sample Footer Text
244>3 so, transverse through the right child of 8

Binary Search Tree :
Monday, February 1, 20XX Sample Footer Text
254<6 so, transverse through the left child of 6

Binary Search Tree :
Monday, February 1, 20XX Sample Footer Text
26Insert 4 as a left child of 6.

Monday, February 1, 20XX Sample Footer Text
27

•General tree
•Binary tree
•Representations
•Tree traversal
•Binary search tree
Monday, February 1, 20XX Sample Footer Text
28

Monday, February 1, 20XX Sample Footer Text
29