0% found this document useful (0 votes)
11 views63 pages

Week8 - Lecture1 and 2-Trees

Uploaded by

hellosaad99
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)
11 views63 pages

Week8 - Lecture1 and 2-Trees

Uploaded by

hellosaad99
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/ 63

Data Structures

Week8_Lecture1 and 2
Tree Data Structures

Subhan Ullah, PhD


[email protected]

BS(Cybersecurity) Fall-2023

Introduction: 1-1
Trees
• A rooted tree data structure stores information in nodes
• Similar to linked lists:
– There is a first node, or root
– Each node has variable number of references to successors
– Each node, other than the root, has exactly one node pointing to it
– There are a number of applications where linear data
structures are not appropriate (e.g., Linear linked list will
not be able to capture the tree-like relationship with
ease)

10-Tree 2
Terminology: Parent Child Relations
• All nodes have zero or more child nodes or children
– “I node” has three children: J, K and L

• For all nodes other than the root node, there is one parent node
– H is the parent of I

10-Tree 3
Terminology: Degree
• The degree of a node is defined as the number of its children
– deg(I) = 3

• Nodes with the same parent are siblings


– J, K, and L are siblings

10-Tree 4
Terminology: Leaf And Internal Nodes
• Nodes with degree zero are also called leaf nodes
• All other nodes are said to be internal nodes, that is, they are
internal to the tree

10-Tree 5
Terminology: Leaf Nodes Examples
• Leaf nodes

10-Tree 6
Terminology: Internal Nodes Example
• Internal nodes

10-Tree 7
Terminology: Path
• A path is a sequence of nodes (a0, a1, ..., an)
– Where ak + 1 is a child of ak

• The length of this path is: n = |nodes in the path| - 1


– For example, the path (B, E, G) has length 2

10-Tree 8
Terminology: Path Example
• Paths of length 10 (11 nodes) and 4 (5 nodes)

Start of these paths

End of these paths

10-Tree 9
Terminology: Depth (or Level)
• For each node in a tree, there exists a unique path from the root
node to that node

• The length of this path is the depth of the node, e.g.,


– E has depth 2
– L has depth 3

10-Tree 10
Terminology: Depth Example
• Nodes of depth up to 17
0

14

17

10-Tree 11
Terminology: Height
• The height of a tree is defined as the maximum depth of any node
within the tree

• The height of a tree with one node is 0


– Just the root node

• For convenience, we define the height of the empty tree to be –1

10-Tree 12
Terminology: Height Example
• Height of this tree is 17

17

10-Tree 13
Terminology: Ancestors And Descendants
• If a path exists from node a to node b
– a is an ancestor of b
– b is a descendent of a

• Thus, a node is both an ancestor and a descendant of itself


– We can add the adjective strict to exclude equality
– a is a strict descendent of b if a is a descendant of b but a ≠ b

• The root node is an ancestor of all nodes

10-Tree 14
Terminology: Ancestors And Descendants Example
• The descendants of node B are C, D, E, F, and G

• The ancestors of node I are H and A

10-Tree 15
Terminology: Descendants Example
• All descendants (including itself) of the indicated node

10-Tree 16
Terminology: Ancestors Example
• All ancestors (including itself) of the indicated node

10-Tree 17
Terminology: Subtree
• Another approach to a tree is to define the tree recursively
– A degree-0 node is a tree

• A node with degree n is a tree if it has n children


– All of its children are disjoint trees (i.e., with no intersecting nodes)

• Given any node a within a tree


with root r, the collection of a and
all of its descendants is said to
be a subtree of the tree with
root a

10-Tree 18
Tree Properties

A
Property Value
Number of nodes : 9
C Height : 4
B Root Node : A
Leaves : D,F,H,I,C
Ancestors of H : G, E, B,A (strict)
Descendants of B : D,E,F,G,H,I(strict)
D E F Siblings of E : D,F
Left subtree : B,D,E,F,G,H,I

H I

10-Tree 19
Example: HTML (1)
• HTML document has a tree structure

<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>

<p>This is a paragraph with some


<u>underlined</u> text.</p>
</body>
</html>

10-Tree 20
Example: HTML (2)
• HTML document has a tree structure

<html> title
<head>
<title>Hello World!</title> heading
</head>
<body>
<h1>This is a <u>Heading</u></h1>
body of page
<p>This is a paragraph with some
<u>underlined</u> text.</p>
</body>
</html>

paragraph
underlining

10-Tree 21
Example: HTML (3)
• The nested tags define a tree rooted at the HTML tag
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>

<p>This is a paragraph with some


<u>underlined</u> text.</p>
</body>
</html>

10-Tree 22
Binary Tree

10-Tree 23
Binary Tree
• In a binary tree each node has at most two children
– Allows to label the children as left and right
• OR: If every non-leaf node in a binary tree has non-empty
left and right subtrees, the tree is termed a strictly
binary tree.

• Likewise, the two sub-trees are referred as


– Left-hand subtree
– Right-hand subtree

10-Tree 24
Binary Tree: Example
• Some variations on binary trees with five nodes

10-Tree 25
Binary Tree: Full Node
• A full node is a node where both the left and right sub-trees are
non-empty trees

full nodes neither leaf nodes

10-Tree 26
Full Binary Tree
• A full binary tree is where each node is:
– A full node, or
– A leaf node
• Full binary tree is also called proper binary tree, strictly binary tree
or 2-tree

10-Tree 27
Complete (Or Perfect) Binary Tree
• A complete binary tree of height h is a binary tree where
– All leaf nodes have the same depth h
– All other nodes are full

10-Tree 28
Level of a Binary Tree Node
• The level of a node in a binary tree is defined as
follows:
 Root has level 0,
 Level of any other node is one more than the level its
parent (father).
• The depth of a binary tree is the maximum level of any
leaf in the tree.A 0 Level 0

B 1 C 1 Level 1

D 2 E 2 F 2 Level 2

G 3 H 3 I 3 Level 3

10-Tree 29
Complete Binary Tree: Recursive Definition
• A complete binary tree of depth d is the strictly
binary all of whose leaves are at level d.

0
A

B 1 C 1

D 2 E 2 F 2 G 2

H 3 I J 3 K L 3 M3 N 3 O 3

10-Tree 30
Complete Binary Tree: Example
• A binary tree of height h = 0 is perfect
• A binary tree with height h > 0 is perfect
– If both sub-trees are prefect binary trees of height h – 1
• Complete binary trees of height h = 0, 1, 2, 3 and 4

10-Tree 31
Data Structures
Week8_Lecture2
Tree Data Structures

Subhan Ullah, PhD


[email protected]

BS(Cybersecurity) Fall-2023

Introduction: 1-32
Complete Binary Tree

A Level 0: 20 nodes

B C Level 1: 21 nodes

D E F G Level 2: 22 nodes

H I J K L M N O Level 3: 23 nodes

10-Tree 33
Binary Tree: Properties (1)
h
• A complete binary tree with height h has 2
___ leaf nodes
• Figure out the answer: The number of nodes in a complete binary
( h+1)
tree with height h is: 2_______ −1

10-Tree 34
Binary Tree: Properties (2)
• A complete binary tree with height h has 2h leaf nodes

• A complete binary tree of height h has 2h + 1


– 1 nodes
h
n  20  21  2 2  ...  2 h   2 j  2 h 1  1
j 0

10-Tree 35
Binary Tree: Properties (3)
• A complete binary tree with height h has 2h leaf nodes

• A complete binary tree of height h has 2h + 1


– 1 nodes
– Number of leaf nodes: L = 2h
– Number of internal nodes: 2h – 1
– Total number of nodes: 2L-1 = 2h + 1
– 1

10-Tree 36
Binary Tree: Properties (4)
• A complete binary tree with height h has 2h leaf nodes

• A complete binary tree of height h has 2h + 1


– 1 nodes
– Number of leaf nodes: L = 2h
– Number of internal nodes: 2h – 1
– Total number of nodes: 2L-1 = 2h + 1
– 1

• A complete binary tree with n nodes has height log2(n + 1) – 1

n  2h  1  1
2h  1  n  1
h  1  log2(n  1)
 h  log2(n  1) - 1
10-Tree 37
Binary Tree: Properties (4)
• A complete binary tree with height h has 2h leaf nodes

• A complete binary tree of height h has 2h + 1


– 1 nodes
– Number of leaf nodes: L = 2h
– Number of internal nodes: 2h – 1
– Total number of nodes: 2L-1 = 2h + 1
– 1

• A complete binary tree with n nodes has height log2(n + 1) – 1

• Number n of nodes in a binary tree of height h is at least h+1


and at most 2h + 1 – 1

10-Tree 38
Almost (or Nearly) Complete Binary Tree
• Almost complete binary tree of height h is a binary tree in which
1. There are 2d nodes at depth d for d = 1,2,...,h−1
 Each leaf in the tree is either at level h or at level h– 1
2. The nodes at depth h are as far left as possible

Complete binary
tree of height
h-1

Missing node towards the right


10-Tree 39
Almost (or Nearly) Complete Binary Tree
• Almost complete binary tree of height h is a binary tree in which
1. There are 2d nodes at depth d for d = 1,2,...,h−1
 Each leaf in the tree is either at level h or at level h– 1
2. The nodes at depth h are as far left as possible (Formal ?)

Complete binary
tree of height
h-1

Missing node towards the right


10-Tree 40
Almost (or Nearly) Complete Binary Tree

Condition 2: The nodes at depth h are as far left as possible


• If a node p at depth h−1 has a left child
– Every node at depth h−1 to the left of p has 2 children
• If a node at depth h−1 has a right child
– It also has a left child

Almost Complete binary tree Not Almost Complete binary tree


(condition 2 violated)
10-Tree 41
Full vs. Almost Complete Binary Tree

10-Tree 42
Almost Complete Binary Tree: Properties
• Total number of nodes n are between
– Complete binary tree of height h-1, i.e., 2h nodes
– Complete binary tree of height h, i.e., 2h+1 -1 nodes

• Height h is the largest integer less than or equal to log2(n)

10-Tree 43
(Completely) Balanced Binary Tree
• Balanced binary tree
– For each node, the difference in height of the right and left sub-trees is
no more than one

• Completely balance binary tree


– Left and right sub-trees of every node have the same height

10-Tree 44
Balanced Binary Tree: Example

a a
b c b

d e f g c e
d f
h i j
g h
A balanced binary tree i j
An unbalanced binary tree

10-Tree 45
(Completely) Balanced Binary Tree
• Balanced binary tree
– For each node, the difference in height of the right and left sub-trees is
no more than one

• When a binary tree is balanced?


– If every level above the lowest is “complete”

• Completely balance binary tree


– Left and right sub-trees of every node have the same height

10-Tree 46
Complete (or Perfect) Binary Tree: Definitions
• A complete binary tree with height h has 2h leaf nodes

• A complete binary tree of height h has 2h + 1


– 1 nodes
– Number of leaf nodes: 2h
– Number of internal nodes: 2h - 1

• A complete binary tree with n nodes has height log2(n + 1) – 1

10-Tree 47
Almost Complete Binary Tree
• A binary tree of height h is an almost complete binary tree if:
– Any node a at depth (or level) less than h-1 has two children
– For any node a in the tree with a right descendant at level h,
 a must have a left child
 Every left child of a is either a leaf at level h or has two children

10-Tree 48
Not a tree
• Structures that are not trees

B C

D E F

G H I

10-Tree 49
Not a tree
• Structures that are not trees

B C

D E F

G H I

10-Tree 50
Data Structures

11. Binary Tree Implementation

11-Tree Implementation 51
Tree ADT
• Data Type: Any type of objects can be stored in a tree

• Accessor methods
– root() – return the root of the tree
– parent(p) – return the parent of a node
– children(p) – return the children of a node

• Query methods
– size() – return the number of nodes in the tree
– isEmpty() – return true if the tree is empty
– elements() – return all elements
– isRoot(p) – return true if node p is the root

• Other methods
– Tree traversal, Node addition/deletion, create/destroy

11-Tree Implementation 52
Binary Tree Storage
• Contiguous storage
• Linked list based storage

11-Tree Implementation 53
Contiguous Storage

11-Tree Implementation 54
Array Storage (1)
• We can store a binary tree as an array

• Traverse tree in breadth-first order, placing the entries into array


– Storage of elements (i.e., objects/data) starts from root node
– Nodes at each level of the tree are stored left to right

11-Tree Implementation 55
Array Storage Example (1)

A
[1] A
[2] B
B C [3] C
[4] D
[5] E
D E F G [6] F
[7] G
[8] H
H I [9] I

11-Tree Implementation 56
Array Storage Example (2)
• Unused nodes in tree represented by a predefined bit pattern

A [1] A
[2] B
[3] -
B
[4] C
[5] -
C
[6] -
[7] -
D [8] D
[9] -
E … …
[16] E
11-Tree Implementation 57
Exercise
• If the following tree is stored in an array, what does the tree look
like? (Draw it)
• ABCDE-F

• Solution:

11-Tree Implementation 58
Array Storage (3)
• The children of the node with index k are in 2k and 2k + 1
• The parent of node with index k is in k ÷ 2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

11-Tree Implementation 59
Array Storage Example (3)
• Node 10 has index 5
– Its children 13 and 23 have indices 10 and 11, respectively

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

11-Tree Implementation 60
Array Storage Example (4)
• Node 10 has index 5
– Its children 13 and 23 have indices 10 and 11, respectively
– Its parent is node 9 with index 5/2 = 2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

11-Tree Implementation 61
Array Storage (4)
• Why array index is not started from 0
– In C++, this simplifies the calculations
parent = k >> 1;
left_child = k << 1;
right_child = left_child | 1;

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

11-Tree Implementation 62
8-Stack 63

You might also like