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

Lec9a BinaryTrees 18042023 090746am

This document discusses binary trees and their implementation. It begins with an introduction to binary trees and their terminology such as root, internal nodes, leaf nodes, and levels. It then provides examples of binary tree structures and discusses different types of binary trees including strictly binary trees and complete binary trees. Finally, it covers two common implementations of binary trees - using static arrays and using dynamically allocated linked nodes.
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)
22 views

Lec9a BinaryTrees 18042023 090746am

This document discusses binary trees and their implementation. It begins with an introduction to binary trees and their terminology such as root, internal nodes, leaf nodes, and levels. It then provides examples of binary tree structures and discusses different types of binary trees including strictly binary trees and complete binary trees. Finally, it covers two common implementations of binary trees - using static arrays and using dynamically allocated linked nodes.
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/ 54

DATA STRUCTURES AND ALGORITHMS

Week 9a: Binary Trees


Qazi Haseeb Yousaf
Dept of CS
Bahria University, Islamabad

1
Introduction

• Data structure such as Arrays, Stacks, Linked List and Queues


are linear data structure. Elements are arranged in linear
manner i.e. one after another.

• Tree is a non-linear data structure.

• Tree imposes a Hierarchical structure, on a


collection of items.

2
A Tree

3
Examples: directory structure

4
Organizational organograms

5
Definition

• A tree is a finite set of nodes, such that:


• There is a special node called the root.
• The remaining nodes are partitioned into n  0 disjoint sets T1, T2, ......., Tn.
• Each of the sets T1, T2, ......., Tn is itself a tree (subtrees of root).

6
ree Terminologies
Root

Level 0 Internal Nodes

Level 1
Arc/Edge

Level 2

Level 3
Parent

Leaf Siblings

7
A

Tree Terminologies
B C
• Root
• It is the key node of a tree structure that does not have
parent. It is the first node in hierarchical arrangement.
• Node
• The node of a tree stores the data and its role is the
same as in the linked list. Nodes are connected by the
means of links with other nodes.
• Parent
• It is the immediate predecessor of a node. In the figure A
is the parent of B and C.

8
A
Tree Terminologies
B C

• Child
• All successor nodes are called child nodes. In the figure B
and C are the child nodes of A
• Sibling
• The child node of same parent are called sibling.
• Link / Edge
• An edge connects the two nodes. The line drawn from one
node to other node is called edge / link. Link is nothing but a
pointer to node in a tree structure.
• Leaf / Terminal Node
• This node is located at the end of the tree. It does not have
any child hence it is called leaf node.
9
Tree Terminologies
• Level
• Level is the rank of tree hierarchy. The whole tree structure is leveled. The level of
the root node is always at 0. the immediate children of root are at level 1 and their
children are at level 2 and so on.
• Depth
• The depth of a node n is the length of the path from the root to the node.
• The depth (or height) of a tree is the length of the path from the root to the
deepest node in the tree.

Root node

Interior nodes Height

Leaf nodes
10
Binary Trees

A binary tree is a tree in which each node has at most 2 children.

root
O

M T
left subtree right subtree
of node O of node O
C E • P U

left subtree right subtree


of node M of node M

11
Binary Trees
If A is the root of a binary tree and B is the root of its left or right subtree,
then A is said to be parent of B, and B is said to be the left or right child
of A.

left child of A B C right child of A

D E • F G


Left child of B right child of B

12
Binary Tree Examples

Binary Trees

76 99

26 85 26

21 50 80 99 21 76
26
50 85
76

80
85

99

13
Binary Trees

Level:

The root of the tree has level 0, and the level of any other node in the tree
is one more than the level of its father.

A
Level 0

B C Level 1

Level 2 D E F G

I J

14
Binary Trees

Depth:

The depth of a binary tree is the maximum level of any


leaf in the tree.

B C

D E F G

Depth: 3 I J

15
Examples – familiarization with notation

16
Types of Binary Trees

• Strictly Binary Trees


• Complete Binary Trees

17
Strictly Binary Tree

If every non-leaf node in a binary tree has nonempty left and right
subtrees, the tree is known as Strictly Binary Tree.

A A

B C B C

D E F G D E F G

H I J I J

Strictly Binary Tree?? Strictly Binary Tree


18
Complete Binary Tree

A complete binary tree of depth d is a strictly binary tree all of


whose leaves are at level d.
A

B C

D E F G

A complete binary tree of depth 2

19
Complete Binary Trees

B C

D E F G

A Complete Binary Tree…


Dept d = 2
No of nodes at level i = 2i
No of leaves = 2d = 22 = 4

20
Complete Binary Trees

B C

D E F G
. . . .
. . . .
. . . .
Total Number of nodes in a complete binary tree:

N = 20+21+22+ ……+2d = j=0 Σ 2


d j

d+1
= 2 -1

21
Complete Binary Trees

Number of nodes in a complete binary tree:

d+1
N= 2 -1

Given the number of nodes of a complete binary tree,


its depth d would be:

d = log2(N + 1) -1

22
Binary Tree Implementation

• Static (Using Arrays)


• Dynamic (Using Linked Nodes)

23
Array-Based Implementation:
An array can be used to store some binary trees.
Number the nodes level by level, from left to right,
Max # nodes on level i:
0
2i
O

1 2 In array representation,
M T children of i are at:
2i + 1, 2i + 2
3 4 5 6
C E • P U Parent of i is at:

• (i - 1) / 2

Store node #0 in array location 0, node #1 in array location 1,


etc.
i 0 1 2 3 4 5 6 . ..
t [i ] O M T C E P U . ..

24
E
But, unless each level of the tree
is full so there are no "dangling
C M
limbs," there can be much
wasted space in the array. U

For example, this binary tree T


contains the same characters
as before but requires 58
___ P

array positions for storage:


O
Max # nodes on level i:
2i i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
t[i] E C M U T
In array representation,
children of i are at: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
2i + 1, 2i + 2 P
Parent of i is at: 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 …
(i - 1) / 2
O …
25
Linked Nodes Implementation:
Use nodes of the form
data

left right

root
Left child Right child

And maintain a pointer to the root. 75

75
60 80
60 80

58 65 92
58 65 92

26
C++ Implementation:
class Node
{
public:
int data;
Node *left, *right;
};
class BinaryTree
{
public:
Node *root; // pointer to root node
BinaryTree();
Node* insert(int);
//Node* search(int); //tree type dependent
//void delete_node(int); //tree type dependent
void traverse(); //pre, in, post order
};

27
BinaryTree::BinaryTree()
{
root=NULL;
}
Node* BinaryTree::insert(int val)
{
Node *p= new Node;
p->data=val;
p->left=NULL;
p->right=NULL;
return p;
}
//Driver.cpp 4
Void main()
{
BinaryTree b; 6 7
b->root = b.insert(4);
b->root->left = b.insert(6); 9
b->root->right = b.insert(7);
b->root->right->left = b.insert(9);
}

28
Implementing Tree
Traversals

29
BINARY TREE ADT

• Create
• Create an empty binary tree
• Empty
• Return true when binary tree is empty else return false.
• Tree Traversal
• Inorder Traversal
• Preorder Traversal
• Postorder Traversal
• Insert
• To insert a node
• Deletion
• To delete a node
• Search
• To search a given node
• Copy
• Copy one tree into another

30
Tree Traversal

Moving through a tree, visiting each node exactly once

• Preorder Traversal
• Inorder Traversal
• Postorder Traversal

31
Traversal of a Binary Tree

• Used to display/access the data in a tree in a certain


order.
• In traversing always right sub-tree is traversed after left
sub-tree.
• Three methods of traversing
• Preorder Traversing
• Root – Left –Right
• Inorder Traversing
• Left – Root – Right
• Postorder Traversing
• Left – Right - Root

32
Traversal of a Binary Tree

33
Tree Traversal

Preodrder:

Visit the root


Traverse the left subtree in preorder
Traverse the right subtree in preorder

Also known as depth-first traversal.

34
Visit the root
Preorder Traversal Traverse the left subtree in preorder
Traverse the right subtree in preorder

32

left subtree 79 42 right subtree

13 95 16

Output: 32
Problem now reduced to traversal of two smaller binary trees.
35
Preorder Traversal

79
Visit root
Traverse left subtree
Traverse right subtree
13

Output: 32 79

•Left subtree is empty


•Now, traverse the right subtree

36
Preorder Traversal

Visit root
13 Traverse left subtree
Traverse right subtree

Output: 32 79 13

37
Preorder Traversal
32

79 42

13 95 16

Output: 32 79 13

38
Preorder Traversal

42 Output: 32 79 13 42

Output: 32 79 13 42 95

Output: 32 79 13 42 95 16
95 16

95 16

39
Preorder Traversal

32

79 42

13 95 16

Output: 32 79 13 42 95 16

40
Preorder Traversal

32

79 42

13 95 16

32 79 13 42 95 16

41
Tree Traversal

Inorder:

Traverse the left subtree in inorder


Visit the root
Traverse the right subtree in inorder

Also known as Symmetric order.

42
Inorder Traversal

32

Traverse the left subtree


Visit the root
Traverse the right subtree

79 42

13 95 16

79 13 32 95 42 16

43
Tree Traversal

Postorder:

Traverse the left subtree in postorder


Traverse the right subtree in postorder
Visit the root

44
Postorder Traversal

Traverse the left subtree


Traverse the right subtree
32 Visit the root

79 42

13 95 16

13 79 95 16 42 32

45
Tree Traversal

Preorder:

root – left – right

Postorder:

left – right – root

Inorder:

left – root – right

46
Tree Traversal

A
Preorder: A B D G C E F H

Inorder: D G B A E C H F
B C
Postorder: G D B E H F C A

D
E F

G
H

47
Tree Traversal

15
16
5
3 20
12
23
10 13 18
6

Pre-order: 15, 5, 3, 12, 10, 6, 7, 13, 16, 20, 18, 23


Post-order: 3, 7, 6, 10, 13, 12, 5, 18, 23, 20, 16, 15
In-order: 3, 5, 6, 7, 10, 12, 13, 15, 16, 18, 20, 23
Tree Traversal

Preorder:

root – left – right

Postorder:

left – right – root

Inorder:
left – root – right

49
Implementing Tree Traversals

void Preorder(Node* ptr)


{
if(ptr==NULL)
{
return;
}
else
{
cout << ptr->data << “ “;
Preorder(ptr->left);
Preorder(ptr->right);
}

}
50
Implementing Tree Traversals

void Postorder(Node* ptr)


{
if(ptr!=NULL)
{
Postorder(ptr->left);
Postorder(ptr->right);
cout << ptr->data << “ “;
}

51
Implementing Tree Traversals

void Inorder(Node* ptr)


{
if(ptr!=NULL)
{
Inorder(ptr->left);
cout << ptr->data << “ “;
Inorder(ptr->right);

52
Practice Tracing The Tree Using The Given Traversal Function

14 Traversal exercises:
finding output
15
4
3 18
9 14
20
7 9 16
5
17
4 5
14 4 3 3 9 7 5 4 4 5 5 5 7 9 9 9 4 15 14 14 18 16 17 17 16 20 20 18 15 14
void Traversal(Node* temp)
{
if(temp!=NULL)
{
cout<<temp->data;
Traversal(temp->left);
Traversal(temp->right);
cout<<temp->data;
}
}

53
Utility functions
75
• Counting the number of leaf nodes:
60 80
Int countleafnodes(Node *ptr)
{ 58 65 92
if(ptr==NULL)
return 0;
if((ptr->left==NULL)&&(ptr->right==NULL))
return 1;
else
return countleafnodes(ptr->left)+coutnleafnodes(ptr->right);
}

54

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy