Professional Documents
Culture Documents
DSA Ch7 Trees
DSA Ch7 Trees
Dr. Nguyen Ho
Man Rang
Chapter 7
Trees Basic Tree
Concepts
Binary Trees
Data Structures and Algorithms Expression Trees
Binary Search
Trees
7.1
Trees
Outcomes
Dr. Nguyen Ho
Man Rang
7.2
Trees
Outcomes
Dr. Nguyen Ho
Man Rang
• L.O.3.6 - Implement binary tree and AVL tree using
C/C++.
• L.O.3.7 - Use binary tree and AVL tree to solve
problems in real-life, especially related to searching
techniques. Basic Tree
Concepts
• L.O.3.8 - Analyze the complexity and develop Binary Trees
experiment (program) to evaluate methods supplied for Expression Trees
tree structures. Binary Search
Trees
• L.O.8.4 - Develop recursive implementations for
methods supplied for the following structures: list, tree,
heap, searching, and graphs.
• L.O.1.2 - Analyze algorithms and use Big-O notation to
characterize the computational complexity of algorithms
composed by using the following control structures:
sequence, branching, and iteration (not recursion).
7.3
Trees
Contents
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
2 Binary Trees Binary Trees
Expression Trees
Binary Search
Trees
3 Expression Trees
7.4
Trees
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Expression Trees
Binary Search
Trees
7.5
Trees
Basic Tree Concepts
Dr. Nguyen Ho
Man Rang
Definition
A tree (cây) consists of a finite set of elements, called nodes
(nút), and a finite set of directed lines, called branches
(nhánh), that connect the nodes.
Basic Tree
Concepts
a Binary Trees
Expression Trees
Binary Search
Trees
b c d
e f g h i
7.6
Trees
Basic Tree Concepts
Dr. Nguyen Ho
Man Rang
• Degree of a node (Bậc của nút): the number of
branches associated with the node.
• Indegree branch (Nhánh vào): directed branch toward
the node.
• Outdegree branch (Nhánh ra): directed branch away Basic Tree
Concepts
from the node.
Binary Trees
Binary Search
• Degree = 4 Trees
• Indegree branches: ad
→ indegree = 1
b c d • Outdegree branches:
dg, dh, di
→ outdegree = 3
e f g h i
7.7
Trees
Basic Tree Concepts
Dr. Nguyen Ho
Man Rang
• The first node is called the root.
• indegree of the root = 0
• Except the root, the indegree of a node = 1
• outdegree of a node = 0 or 1 or more.
Basic Tree
Concepts
Expression Trees
Binary Search
Trees
b c d
e f g h i
7.8
Trees
Basic Tree Concepts
Dr. Nguyen Ho
Terms Man Rang
Terms
• The level (bậc) of a node is its distance from the root. Binary Trees
Binary Search
Trees
• The height (độ cao) of a tree is the level of the leaf in
the longest path from the root plus 1.
7.10
Trees
Basic Tree Concepts
Dr. Nguyen Ho
Level 0 a Man Rang
Branch ad
Level 1 b c d
Basic Tree
Concepts
Expression Trees
a
Subtree b Subtree d
Basic Tree
Concepts
Binary Trees
b c d Expression Trees
Binary Search
Trees
e f g h i
7.12
Trees
Tree representation
Dr. Nguyen Ho
Man Rang
Binary Trees
e f g h i f Expression Trees
c Binary Search
Trees
• parenthetical listing d
g
a (b (e f ) c d (g h i))
h
i
7.13
Trees
Applications of Trees
Dr. Nguyen Ho
Man Rang
Binary Trees
searchable (ex: binary search tree) Expression Trees
Binary Search
7.14
Trees
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Expression Trees
Binary Search
Trees
7.15
Trees
Binary Trees
Dr. Nguyen Ho
Man Rang
a
Basic Tree
Left subtree Right subtree Concepts
Binary Trees
Expression Trees
Binary Search
b d Trees
e f g
7.16
Trees
Binary Trees Properties
Dr. Nguyen Ho
• To store N nodes in a binary tree: Man Rang
• The minimum height: Hmin = blog2 N c + 1 or
Hmin = dlog2 (N + 1)e
• The maximum height: Hmax = N
Expression Trees
Balance Binary Search
Trees
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 7.17
Trees
Binary Trees Properties
Dr. Nguyen Ho
Man Rang
Complete tree
N = Nmax = 2H − 1 a
The last level is full.
b c Basic Tree
Concepts
d e f g Binary Trees
Expression Trees
Binary Search
Trees
Nearly complete tree
H = Hmin = blog2 N c + 1 a
Nodes in the last level are on
the left. b c
d e
7.18
Trees
Binary Tree Structure
Dr. Nguyen Ho
Definition Man Rang
Binary Trees
Expression Trees
Binary Search
Trees
7.19
Trees
Binary Tree Structure: Linked implementation
Dr. Nguyen Ho
Man Rang
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 Basic Tree
Concepts
Binary Trees
// G e n e r a l d a t a T y e : Expression Trees
dataType
k e y <keyType> Binary Search
Trees
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
7.20
Trees
Binary Tree Structure: Array-based implementation
Dr. Nguyen Ho
Suitable for complete tree, nearly complete tree. Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
Hình: 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
Hình: Physical
7.21
Trees
Binary Tree Traversals
Dr. Nguyen Ho
Man Rang
Binary Search
Trees
• Breadth-first traversal (duyệt theo chiều rộng): 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.
7.22
Trees
Depth-first traversal
Dr. Nguyen Ho
Man Rang
• Preorder traversal
• Inorder traversal
• Postorder traversal
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.23
Trees
Preorder traversal (NLR)
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.24
Trees
Preorder traversal (NLR)
Dr. Nguyen Ho
Man Rang
Expression Trees
Post: each node has been processed in Binary Search
Trees
order
if root is not null then
process(root)
preOrder(root->left)
preOrder(root->right)
end
Return 7.25
Trees
Inorder traversal (LNR)
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.26
Trees
Inorder traversal (LNR)
Dr. Nguyen Ho
Man Rang
Expression Trees
Post: each node has been processed in Binary Search
Trees
order
if root is not null then
inOrder(root->left)
process(root)
inOrder(root->right)
end
Return 7.27
Trees
Postorder traversal (LRN)
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.28
Trees
Postorder traversal (LRN)
Dr. Nguyen Ho
Man Rang
Expression Trees
Post: each node has been processed in Binary Search
Trees
order
if root is not null then
postOrder(root->left)
postOrder(root->right)
process(root)
end
Return 7.29
Trees
Breadth-First Traversals
Dr. Nguyen Ho
Man Rang
Binary Trees
Expression Trees
Binary Search
Trees
7.30
Trees
Breadth-First Traversals
Dr. Nguyen Ho
Man Rang
Expression Trees
currentNode = root
bfQueue = createQueue()
7.31
Trees
Breadth-First Traversals
Dr. Nguyen Ho
Man Rang
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Expression Trees
Binary Search
Trees
7.33
Trees
Expression Trees
Dr. Nguyen Ho
Man Rang
Binary Trees
Expression Trees
Binary Search
Trees
7.34
Trees
Infix Expression Tree Traversal
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.35
Trees
Infix Expression Tree Traversal
Dr. Nguyen Ho
Man Rang
Binary Trees
Post: the postfix expression has been Expression Trees
Binary Search
if tree not empty then Trees
print (tree->data)
prefix (tree->left)
prefix (tree->right)
end
End prefix
7.38
Trees
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Expression Trees
Binary Search
Trees
7.39
Trees
Binary Search Trees
Dr. Nguyen Ho
Man Rang
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 Basic Tree
Concepts
to the root.
Binary Trees
3 Each subtree is itself a binary search tree. Expression Trees
Binary Search
Trees
7.40
Trees
Valid Binary Search Trees
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.41
Trees
Invalid Binary Search Trees
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.42
Trees
Binary Search Tree (BST)
Dr. Nguyen Ho
Man Rang
Binary Search
binary search on a contiguous list). Trees
7.43
Trees
Binary Search Tree Traversals
Dr. Nguyen Ho
Man Rang
23
18 44
Basic Tree
Concepts
Binary Trees
12 20 35 52 Expression Trees
Binary Search
Trees
Binary Trees
Pre: root is a pointer to a nonempty BST Expression Trees
Binary Trees
Pre: root is a pointer to a nonempty BST Expression Trees
value. Concepts
Binary Trees
Expression Trees
Pre: root is the root to a binary tree or Binary Search
Trees
subtree
target is the key value requested
Binary Search
else if target > root->data.key then Trees
End searchBST
7.48
Trees
Binary Search
Dr. Nguyen Ho
Man Rang
Iterative Search
Algorithm iterativeSearchBST(val root
<pointer>, val target <keyType>)
Search a binary search tree for a given Basic Tree
Binary Trees
Expression Trees
Pre: root is the root to a binary tree or Binary Search
Trees
subtree
target is the key value requested
Iterative Search
while (root is not NULL) AND
(root->data.key <> target) do
Basic Tree
if target < root->data.key then Concepts
Expression Trees
else Binary Search
root = root->right Trees
end
end
return root
End iterativeSearchBST
7.50
Trees
Insert Node into BST
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
Expression Trees
7.52
Trees
Insert Node into BST: Iterative Insert
Dr. Nguyen Ho
if root is null then Man Rang
root = new
else
pWalk = root
while pWalk not null do
parent = pWalk
Basic Tree
if new->data.key < pWalk->data.key then Concepts
pWalk = pWalk->left Binary Trees
else Expression Trees
pWalk = pWalk->right Binary Search
Trees
end
end
if new->data.key < parent->data.key then
parent->left = new
else
parent->right = new
end
end
End iterativeInsertBST 7.53
Trees
Insert Node into BST: Recursive Insert
Dr. Nguyen Ho
Man Rang
Binary Trees
Expression Trees
Pre: root is address of current node in a Binary Search
Trees
BST
new is address of node containing data to
be inserted
7.54
Trees
Insert Node into BST: Recursive Insert
Dr. Nguyen Ho
Man Rang
if root is null then
root = new
else
if new->data.key < root->data.key
Basic Tree
then Concepts
Expression Trees
else Binary Search
recursiveInsertBST(root->right, Trees
new)
end
end
Return
End recursiveInsertBST
7.55
Trees
Delete node from BST
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
Basic Tree
Deletion of a node having both subtrees: Concepts
Binary Trees
Replace the deleted node by its predecessor or Expression Trees
7.58
Trees
Delete node from BST
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.59
Trees
Delete node from BST
Dr. Nguyen Ho
Man Rang
Basic Tree
Concepts
Binary Trees
Expression Trees
Binary Search
Trees
7.60
Trees
Delete node from BST
Dr. Nguyen Ho
Man Rang
Algorithm deleteBST(ref root <pointer>,
val dltKey <keyType>)
Deletes a node from a BST.
Basic Tree
Pre: root is pointer to tree containing Concepts
Binary Trees
data to be deleted Expression Trees
7.62
Trees
Delete node from BST
Dr. Nguyen Ho
Man Rang
else
// Deleted node found – Test for leaf
node
if root->left is null then Basic Tree
dltPtr = root Concepts
Binary Trees
root = root->right Expression Trees
return true
else if root->right is null then
dltPtr = root
root = root->left
recycle(dltPtr)
return true
7.63
Trees
Delete node from BST
Dr. Nguyen Ho
else Man Rang
// ...
else
// Deleted node is not a leaf.
// Find largest node on left subtree
dltPtr = root->left Basic Tree
Concepts
while dltPtr->right not null do Binary Trees
dltPtr = dltPtr->right Expression Trees
end Binary Search
Trees
// Node found. Move data and delete leaf
node
root->data = dltPtr->data
return deleteBST(root->left,
dltPtr->data.key)
end
end
End deleteBST
7.64