Académique Documents
Professionnel Documents
Culture Documents
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
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
345
345 Next
A[] 𝟏𝟗 𝟒𝟕 𝟖𝟔 345 𝟓𝟏𝟑
Array
slower than
513 NULL
Ordered Linked List
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
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
Root Node
BASIC TREE TERMINOLOGY
[1]
A
Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A
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
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
Root Node
BASIC TREE TERMINOLOGY
[1]
[Contd.] A
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
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
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)
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)
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
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}
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
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
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
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.
Root Node
ORGANIZING A TREE edge
A
Root Node
TREE TRAVERSAL A
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
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
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.