Vous êtes sur la page 1sur 28

FACULTY OF SCIENCE AND

INFORMATION TECHNOLOGY
Phnom Penh International University

Mr. S A R V A T H N A K
June, 2019
PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

OBJECTIVES
 To introduce a non-linear data structure, which is mainly used
for storing data in hierarchical nature, and its terminology
 To understand 2 common kinds of tree
(Binary Tree & Binary Search Tree)
 To demonstrate storage representation of a binary tree
 To implement any tree operations by using recursion
and non-recursion
 To demonstrate tree traversals

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 1


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
INTRODUCTION A

 What is tree? D B C
 Tree is a collection of nodes which is linked from
one parent node to another children nodes
in a recursive way.
E F G H I J
 Comparing tree with doubly linked list:
 For doubly linked list: Each node always has M N K L
one previous node and one next node,
except the first node and the last node.
 For tree: Each node always has one previous node and
possibly has either no next node or more next nodes, External Pointer
except the root node.
pList

NULL Info 1 Next Prev Info 2 Next Prev Info... Next Prev Info N NULL

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 2


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

WHY NEEDS TREE?


 Tree is preferred to use because:
 you want to store information
that naturally forms a hierarchy
(Ex. The file system on a computer)

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 3a


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

WHY NEEDS TREE? root pList


19

 Tree is preferred to use because: 19 Next


 you want to store information 86
that naturally forms a hierarchy
(Ex. The file system on a computer) 47 Next
 trees (with some ordering e.g., BST) 47 513
provide moderate access/search
(quicker than Linked Lists and
slower than Arrays) Binary Search Tree 86 Next

345
345 Next
A[] 𝟏𝟗 𝟒𝟕 𝟖𝟔 345 𝟓𝟏𝟑
Array
slower than
513 NULL
Ordered Linked List

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 3b


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

WHY NEEDS TREE? root pList


86
86 Next
 Tree is preferred to use because:
47 513
 you want to store information
that naturally forms a hierarchy 47 Next
(Ex. The file system on a computer) General Tree
 trees (with some ordering e.g., BST)
provide moderate access/search 19 345 513 Next
(quicker than Linked Lists and
slower than Arrays)
 trees provide moderate insertion/deletion 19 Next
(quicker than Arrays and
slower than Unordered Linked Lists)
345 NULL
A[] 86 47 513 19 345 Linked List
Array

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 3c


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

pList
WHY NEEDS TREE? 86
root

86 Next
 Tree is preferred to use because:
47 513
 you want to store information
that naturally forms a hierarchy 47 Next
(Ex. The file system on a computer) General Tree
 trees (with some ordering e.g., BST)
provide moderate access/search 19 345 513 Next
(quicker than Linked Lists and
slower than Arrays)
 trees provide moderate insertion/deletion 19 Next
(quicker than Arrays and
slower than Unordered Linked Lists)
 trees don’t have an upper limit (as in array) 345 NULL
on number of nodes as nodes are linked
using pointers (like linked list) Linked List
Array
A[5] 86 47 513 19 345
TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 3d
PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY edge
[1]
A

 Root node having no predecessor D B C

 All the nodes in a tree, except root node,


having only one predecessor
E F G H I J
 Every node in a tree has two parts:
1. Item: value stored in a node
M N K L
2. Edge(s) : directed pointer that connect
from parent node to its children nodes

 In Degree:
 is a number of edges comes into the particular node

 Out Degree:
 is a number of edges comes out from the particular node

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 4a


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
A

 Root node having no predecessor D B C

 All the nodes in a tree, except root node,


having only one predecessor
E F G H I J
 Every node in a tree has two parts:
1. Item: value stored in a node
M N K L
2. Edge(s) : directed pointer that connect
from parent node to its children nodes
Examples:
 In Degree: In Degree(A) = 0
 is a number of edges comes into the particular node Out Degree(A) = 3
 Out Degree:
In Degree(M) = 1
 is a number of edges comes out from the particular node
Out Degree(M) = 0

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 4b


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Degree or Total Degree: D B C


 is the sum of edges comes into the
particular node and edges comes out from
the particular node
E F G H I J
i.e., Degree = In Degree + Out Degree

M N K L

Examples:
In Degree(A) = 0
Out Degree(A) = 3
 Degree(A) = 0 + 3 = 3

In Degree(F) = 1
Out Degree(F) = 2
 Degree(M) = 1 + 2 = 3
TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 5a
PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Degree or Total Degree: D B C


 is the sum of edges comes into the
particular node and edges comes out from
the particular node
E F G H I J
i.e., Degree = In Degree + Out Degree

 Root Node: M N K L
 is a node having In Degree equal to 0

 Leaf Node:
 is a node having Out Degree equal to 0

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 5b


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Degree or Total Degree: D B C


 is the sum of edges comes into the
particular node and edges comes out from
the particular node
E F G H I J
i.e., Degree = In Degree + Out Degree

 Root Node: M N K L
 is a node having In Degree equal to 0

 Leaf Node:
 is a node having Out Degree equal to 0

 Path:
 is a sequence of connecting edges from one node to another node
 Ex: A  D  F  N is a path from A to N

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 5c


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Depth: D B C
 is the length of the path from root node to
the particular node.
Ex: Depth(G) = 2, Depth(L) = 3 E F G H I J
 Note: Depth of the tree can be defined as
the length of the path from root node to
the deepest node in the tree. M N K L
Ex: Depth of the tree = 3

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 6a


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Depth: D B C
 is the length of the path from root node to
the particular node.
Ex: Depth(G) = 2, Depth(L) = 3 E F G H I J
 Note: Depth of the tree can be defined as
the length of the path from root node to
the deepest node in the tree. M N K L
Ex: Depth of the tree = 3

 Binary Tree:
 is a tree in which out degree of each node is less then or equal to 2
but not less then 0 (i.e., 0 ≤ 𝑜𝑢𝑡 𝑑𝑒𝑔𝑟𝑒𝑒 ≤ 2)

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 6b


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Depth: D B C
 is the length of the path from root node to
the particular node.
Ex: Depth(G) = 2, Depth(L) = 3 E F G H I J
 Note: Depth of the tree can be defined as
the length of the path from root node to
the deepest node in the tree. M N K L
Ex: Depth of the tree = 3

 Binary Tree:
 is a tree in which out degree of each node is less then or equal to 2
but not less then 0 (i.e., 0 ≤ 𝑜𝑢𝑡 𝑑𝑒𝑔𝑟𝑒𝑒 ≤ 2)

 Complete Binary Tree:


 is a tree in which out degree of each node is exactly equals to 0 or 2

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 6c


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Parent Node: D B C
 is a node that stays before another node(s)
 Ex: D is a parent node of Nodes {E, F, G}
E F G H I J
 Children Node:
 is a node that stays after another node
 Ex: E, F, and G are children nodes of node D M N K L

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 7a


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Parent Node: D B C
 is a node that stays before another node(s)
 Ex: D is a parent node of Nodes {E, F, G}
E F G H I J
 Children Node:
 is a node that stays after another node
 Ex: E, F, and G are children nodes of node D M N K L

 Grandparent Node:
 is a parent of parent of the particular node(s)
 Ex: C is a grandparent node of Nodes {K, L}

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 7b


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Parent Node: D B C
 is a node that stays before another node(s)
 Ex: D is a parent node of Nodes {E, F, G}
E F G H I J
 Children Node:
 is a node that stays after another node
 Ex: E, F, and G are children nodes of node D M N K L

 Grandparent Node:
 is a parent of parent of the particular node(s)
 Ex: C is a grandparent node of Nodes {K, L}

 Sibling Node:
 is a node that has the same parent node
 Ex: E and F are sibling nodes with node G

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 7c


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Ancestor Node: D B C
 is a parent node of the particular node or
is a parent ancestor of the particular node
 Ex: F, D, and A are ancestor nodes of E F G H I J
Nodes {M, N}

M N K L

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 8a


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Ancestor Node: D B C
 is a parent node of the particular node or
is a parent ancestor of the particular node
 Ex: F, D, and A are ancestor nodes of E F G H I J
Nodes {M, N}

 Descendent Node: M N K L
 is a children node of the particular node or
is a children descendent node of the particular node
 Ex: H, I, J, K, L are descendent nodes of node C

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 8b


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A

 Ancestor Node: D B C
 is a parent node of the particular node or
is a parent ancestor of the particular node
 Ex: F, D, and A are ancestor nodes of E F G H I J
Nodes {M, N}
is a sub tree of node D
 Descendent Node: M N K L
 is a children node of the particular node or
is a children descendent node of the particular node
 Ex: H, I, J, K, L are descendent nodes of node C is a sub tree of node A
 Sub Tree:
 is one child node with all descendent nodes of the particular node
 Note: There may exist another sub tree in one sub tree.

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 8c


PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
ORGANIZING A TREE edge
A

 A tree can be organized into 3 different ways: D B C


 Organizing tree in a diagram
 Organizing tree in parentheses
 Organizing tree in vertical-line list E F G H I J
A
D
E
M N K L
F M
N
G
B
C A(D(E)(F(M)(N))(G)(B)(C(H)(I(K)(L))(J))
H
I K
L
J
TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 9
PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
TREE TRAVERSAL A

 We could search a tree in two ways: D B C


 Breath First Search (BFS)
 Depth First Search (DFS)
E F G H I J

M N K L

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 10a
PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
TREE TRAVERSAL A

 We could search a tree in two ways: D B C


 Breath First Search (BFS)is a process of
searching nodes by firstly going through
a root node, then searching through
E F G H I J
all children nodes of the root node and
repeatedly doing the same way until
we reach the last nodes in the last level. M N K L
 Depth First Search (DFS)

Result of BFS Tree Traversal: A D B C E F G H I J M N K L

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 10b
PHNOM PENH INTERNATIONAL UNIVERSITY FACULTY OF SCIENCE AND INFORMATION TECHNOLOGY

Root Node
TREE TRAVERSAL A

 We could search a tree in two ways: D B C


 Breath First Search (BFS)
 Depth First Search (DFS)is a process of
searching nodes by firstly going through E F G H I J
a root node, then going through sub-tree
of the first child of the root node, then
going through sub tree of the second child M N K L
of the root node, and repeatedly doing
the same way until we reach the sub-tree
of the last child node of the root node.

Result of DFS Tree Traversal: A D E F M N G B C H I K L J

TREE DATA STRUCTURE AND ALGORITHM (II) | BY SAR VATHNAK >> 10c
REFERENCES
[1] The Code Gallery, “Basic Tree Concepts and Definitions”, Data structure tutorials.
[Available at] http://thecodegallery.com/DSM/Tree.php.
[Accessed on] February, 2018.
[2] GeeksforGeeks, “Binary Tree | Set 1 (Introduction)”, Data structure tutorials.
[Available at] https://www.geeksforgeeks.org/binary-tree-set-1-introduction/
[Accessed on] February, 2018.
[3] Reema Thareja, “Data Structures using C”, 2nd Edition. 2014.
[4] Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser, “Data Structures &
Algorithms in Java”. 6th Edition, Wiley. 2014.

Vous aimerez peut-être aussi