Week8 - Lecture1 and 2-Trees
Week8 - Lecture1 and 2-Trees
Week8_Lecture1 and 2
Tree Data Structures
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
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
10-Tree 8
Terminology: Path Example
• Paths of length 10 (11 nodes) and 4 (5 nodes)
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
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
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
10-Tree 14
Terminology: Ancestors And Descendants Example
• The descendants of node B are C, D, E, F, and G
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
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>
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>
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.
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
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
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
10-Tree 35
Binary Tree: Properties (3)
• A complete binary tree with height h has 2h leaf nodes
10-Tree 36
Binary Tree: Properties (4)
• A complete binary tree with height h has 2h leaf nodes
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
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
Complete binary
tree of height
h-1
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
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
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
10-Tree 46
Complete (or Perfect) Binary Tree: Definitions
• A complete binary tree with height h has 2h leaf nodes
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-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
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