Professional Documents
Culture Documents
Data Structure and Algorithms (CO2003) : Chapter 6 - Tree
Data Structure and Algorithms (CO2003) : Chapter 6 - Tree
Chapter 6 - Tree
2. Binary Trees
3. Expression Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 1 / 59
Outcomes
• L.O.3.1 - Depict the following concepts: binary tree, complete binary tree, balanced
binary tree, AVL tree, multi-way tree, etc.
• L.O.3.2 - Describe the strorage structure for tree structures using pseudocode.
• L.O.3.3 - List necessary methods supplied for tree structures, and describe them using
pseudocode.
• L.O.3.4 - Identify the importance of “blanced” feature in tree structures and give
examples to demonstate it.
• L.O.3.5 - Identiy cases in which AVL tree and B-tree are unblanced, and demonstrate
methods to resolve all the cases step-by-step using figures.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 2 / 59
Outcomes
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 3 / 59
Basic Tree Concepts
Basic Tree Concepts
Definition
A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called
branches, that connect the nodes.
b c d
e f g h i
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 4 / 59
Basic Tree Concepts
e f g h i
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 5 / 59
Basic Tree Concepts
b c d
e f g h i
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 6 / 59
Basic Tree Concepts
Terms
• A root is the first node with an indegree of zero.
• A leaf is any node with an outdegree of zero.
• A internal node is not a root or a leaf.
• A parent has an outdegree greater than zero.
• A child has an indegree of one.
→ a internal node is both a parent of a node and a child of another one.
• Siblings are two or more nodes with the same parent.
• For a given node, an ancestor is any node in the path from the root to the node.
• For a given node, an descendent is any node in the paths from the node to a leaf.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 7 / 59
Basic Tree Concepts
Terms
• A path is a sequence of nodes in which each node is adjacent to the next one.
• The height of a tree is the level of the leaf in the longest path from the root plus 1.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 59
Basic Tree Concepts
Level 0 a
Branch ad
Level 1 b c d
Branch di
Level 2 e f g h i
a
Subtree b Subtree d
b c d
e f g h i
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 10 / 59
Tree representation
• organization chart a
a b
e
b c d
f
e f g h i c
d
• parenthetical listing g
a (b (e f ) c d (g h i))
h
• indented list i
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 11 / 59
Applications of Trees
• Storing data in a way that makes it easily searchable (ex: binary search tree)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 12 / 59
Binary Trees
Binary Trees
a
Left subtree Right subtree
b d
e f g
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 13 / 59
Binary Trees Properties
Balance
The balance factor of a binary tree is the difference in height between its left and right subtrees.
B = HL − HR
Balanced tree:
• balance factor is 0, -1, or 1
• subtrees are balanced
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 14 / 59
Binary Trees Properties
Complete tree
N = Nmax = 2H − 1 a
The last level is full.
b c
d e f g
d e
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 15 / 59
Binary Tree Structure
Definition
A binary tree is either empty, or it consists of a node called root together with two binary trees
called the left and the right subtree of the root.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 59
Binary Tree Structure: Linked implementation
node binaryTree
d a t a <dataType> r o o t <p o i n t e r >
l e f t <p o i n t e r > end b i n a r y T r e e
r i g h t <p o i n t e r >
end node
// G e n e r a l d a t a T y e :
dataType
k e y <keyType>
f i e l d 1 <... >
f i e l d 2 <... >
...
f i e l d n <... >
end d a t a T y p e
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 17 / 59
Binary Tree Structure: Array-based implementation
Figure 1: Conceptual
binaryTree
d a t a <a r r a y o f dataType>
end b i n a r y T r e e
• Depth-first traversal: the processing proceeds along a path from the root through one
child to the most distant descendent of that first child before processing a second child,
i.e. processes all of the descendents of a child before going on to the next child.
• Breadth-first traversal: the processing proceeds horizontally from the root to all of its
children, then to its children’s children, i.e. each level is completely processed before the
next level is started.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 19 / 59
Depth-first traversal
• Preorder traversal
• Inorder traversal
• Postorder traversal
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 59
Preorder traversal (NLR)
In the preorder traversal, the root is processed first, before the left and right subtrees.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 21 / 59
Preorder traversal (NLR)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 22 / 59
Inorder traversal (LNR)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 23 / 59
Inorder traversal (LNR)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 24 / 59
Postorder traversal (LRN)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 25 / 59
Postorder traversal (LRN)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 26 / 59
Breadth-First Traversals
In the breadth-first traversal of a binary tree, we process all of the children of a node before
proceeding with the next level.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 27 / 59
Breadth-First Traversals
currentNode = root
bfQueue = createQueue()
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 59
Breadth-First Traversals
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 59
Infix Expression Tree Traversal
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 59
Infix Expression Tree Traversal
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 59
Prefix Expression Tree Traversal
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 34 / 59
Binary Search Trees
Binary Search Trees
Definition
A binary search tree is a binary tree with the following properties:
1. All items in the left subtree are less than the root.
2. All items in the right subtree are greater than or equal to the root.
3. Each subtree is itself a binary search tree.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 35 / 59
Valid Binary Search Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 36 / 59
Invalid Binary Search Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 37 / 59
Binary Search Tree (BST)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 38 / 59
Binary Search Tree Traversals
23
18 44
12 20 35 52
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 40 / 59
Binary Search Tree Search
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 41 / 59
Binary Search
Recursive Search
Algorithm searchBST(val root <pointer>, val target <keyType>)
Search a binary search tree for a given value.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 42 / 59
Binary Search
Recursive Search
if root is null then
return null
end
End searchBST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 43 / 59
Binary Search
Iterative Search
Algorithm iterativeSearchBST(val root <pointer>, val target <keyType>)
Search a binary search tree for a given value using a loop.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 44 / 59
Binary Search
Iterative Search
while (root is not NULL) AND (root->data.key <> target) do
if target < root->data.key then
root = root->left
else
root = root->right
end
end
return root
End iterativeSearchBST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 45 / 59
Insert Node into BST
All BST insertions take place at a leaf or a leaflike node (a node that has only one null branch).
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 46 / 59
Insert Node into BST: Iterative Insert
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 47 / 59
Insert Node into BST: Iterative Insert
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 49 / 59
Insert Node into BST: Recursive Insert
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 50 / 59
Delete node from BST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 51 / 59
Delete node from BST
Deletion of a node having only right subtree or left subtree: Attach the subtree to the deleted
node’s parent.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 52 / 59
Delete node from BST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 53 / 59
Delete node from BST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 54 / 59
Delete node from BST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 55 / 59
Delete node from BST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 56 / 59
Delete node from BST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 57 / 59
Delete node from BST
else
// Deleted node found – Test for leaf node
if root->left is null then
dltPtr = root
root = root->right
recycle(dltPtr)
return true
else if root->right is null then
dltPtr = root
root = root->left
recycle(dltPtr)
return true
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 58 / 59
Delete node from BST
else
// ...
else
// Deleted node is not a leaf.
// Find largest node on left subtree
dltPtr = root->left
while dltPtr->right not null do
dltPtr = dltPtr->right
end
// Node found. Move data and delete leaf node
root->data = dltPtr->data
return deleteBST(root->left, dltPtr->data.key)
end
end
End deleteBST
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 59 / 59