Professional Documents
Culture Documents
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.1
Tree concepts
Overview
Dept. Computer
Science
Binary Trees
Expression Trees
2 Binary Trees
Binary Search Trees
3 Expression Trees
Tree concepts.2
Tree concepts
Dept. Computer
Science
Binary Trees
Tree concepts.3
Tree concepts
Basic Tree Concepts
Dept. Computer
Science
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
Basic Tree Concepts
(nhánh), that connect the nodes.
Binary Trees
Expression Trees
a Binary Search Trees
b c d
e f g h i
Tree concepts.4
Tree concepts
Basic Tree Concepts
Dept. Computer
Science
• 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. Basic Tree Concepts
e f g h i
Tree concepts.5
Tree concepts
Basic Tree Concepts
Dept. Computer
Science
• The first node is called the root.
• indegree of the root = 0
• Except the root, the indegree of a node = 1
Basic Tree Concepts
• outdegree of a node = 0 or 1 or more. Binary Trees
Expression Trees
a root Binary Search Trees
b c d
e f g h i
Tree concepts.6
Tree concepts
Basic Tree Concepts
Dept. Computer
Terms Science
Terms
• A path (đường đi) is a sequence of nodes in which Basic Tree Concepts
Expression Trees
• The level (bậc) of a node is its distance from the root. Binary Search Trees
Tree concepts.8
Tree concepts
Basic Tree Concepts
Dept. Computer
Level 0 a Science
Branch ad
Basic Tree Concepts
Level 1 b c d Binary Trees
Expression Trees
Branch di Binary Search Trees
Level 2 e f g h i
a
Subtree b Subtree d Basic Tree Concepts
Binary Trees
Expression Trees
e f g h i
Tree concepts.10
Tree concepts
Tree representation
Dept. Computer
Science
• organization chart a
a b
Basic Tree Concepts
e Binary Trees
b c d Expression Trees
f Binary Search Trees
e f g h i c
d
• parenthetical listing g
a (b (e f ) c d (g h i))
h
i
• indented list
Tree concepts.11
Tree concepts
Applications of Trees
Dept. Computer
Science
Binary Trees
• Storing data in a way that makes it easily Expression Trees
Tree concepts.12
Tree concepts
Dept. Computer
Science
Binary Trees
Tree concepts.13
Tree concepts
Binary Trees
Dept. Computer
Science
b d
e f g
Tree concepts.14
Tree concepts
Binary Trees Properties
Dept. Computer
• To store N nodes in a binary tree: Science
• The minimum height: Hmin = blog2 N c + 1 or
Hmin = dlog2 (N + 1)e
• The maximum height: Hmax = N
Basic Tree Concepts
• Given a height of the binary tree, H: Binary Trees
• The minimum number of nodes: Nmin = H Expression Trees
• The maximum number of nodes: Nmax = 2H − 1 Binary Search Trees
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 Tree concepts.15
Tree concepts
Binary Trees Properties
Dept. Computer
Science
Complete tree
N = Nmax = 2H − 1 a
Basic Tree Concepts
The last level is full.
b c Binary Trees
Expression Trees
H = Hmin = blog2 N c + 1 a
Nodes in the last level are
on the left. b c
d e
Tree concepts.16
Tree concepts
Binary Tree Structure
Dept. Computer
Science
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. Basic Tree Concepts
Binary Trees
Expression Trees
Tree concepts.17
Tree concepts
Binary Tree Structure: Linked implementation
Dept. Computer
Science
node binaryTree
d a t a <dataType> r o o t <p o i n t e r >
Basic Tree Concepts
l e f t <p o i n t e r > end b i n a r y T r e e
Binary Trees
r i g h t <p o i n t e r >
Expression Trees
end node
Binary Search Trees
// 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
Tree concepts.18
Tree concepts
Binary Tree Structure: Array-based implementation
Dept. Computer
Suitable for complete tree, nearly complete tree. Science
Binary Trees
Expression 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
Tree concepts.19
Tree concepts
Binary Tree Traversals
Dept. Computer
Science
Tree concepts.20
Tree concepts
Depth-first traversal
Dept. Computer
Science
• Preorder traversal
• Inorder traversal
Basic Tree Concepts
• Postorder traversal Binary Trees
Expression Trees
Tree concepts.21
Tree concepts
Preorder traversal (NLR)
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.22
Tree concepts
Preorder traversal (NLR)
Dept. Computer
Science
Binary Trees
3 Pre: root is the entry node of a tree or subtree
Expression Trees
4 Post: each node has been processed in order
Binary Search Trees
5 if root is not null then
6 process(root)
7 preOrder(root->left)
8 preOrder(root->right)
9 end
10 Return
11 End preOrder
Tree concepts.23
Tree concepts
Inorder traversal (LNR)
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.24
Tree concepts
Inorder traversal (LNR)
Dept. Computer
Science
Binary Trees
3 Pre: root is the entry node of a tree or subtree
Expression Trees
4 Post: each node has been processed in order
Binary Search Trees
5 if root is not null then
6 inOrder(root->left)
7 process(root)
8 inOrder(root->right)
9 end
10 Return
11 End inOrder
Tree concepts.25
Tree concepts
Postorder traversal (LRN)
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.26
Tree concepts
Postorder traversal (LRN)
Dept. Computer
Science
Binary Trees
3 Pre: root is the entry node of a tree or subtree
Expression Trees
4 Post: each node has been processed in order
Binary Search Trees
5 if root is not null then
6 postOrder(root->left)
7 postOrder(root->right)
8 process(root)
9 end
10 Return
11 End postOrder
Tree concepts.27
Tree concepts
Breadth-First Traversals
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.28
Tree concepts
Breadth-First Traversals
Dept. Computer
Science
5 currentNode = root
6 bfQueue = createQueue()
Tree concepts.29
Tree concepts
Breadth-First Traversals
Dept. Computer
Science
Dept. Computer
Science
Binary Trees
Tree concepts.31
Tree concepts
Expression Trees
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.32
Tree concepts
Infix Expression Tree Traversal
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.33
Tree concepts
Infix Expression Tree Traversal
Dept. Computer
Science
Tree concepts.35
Tree concepts
Prefix Expression Tree Traversal
Dept. Computer
Science
Tree concepts.36
Tree concepts
Dept. Computer
Science
Binary Trees
Tree concepts.37
Tree concepts
Binary Search Trees
Dept. Computer
Science
Definition
A binary search tree is a binary tree with the following prop-
erties:
1 All items in the left subtree are less than the root. Basic Tree Concepts
Binary Trees
2 All items in the right subtree are greater than or equal
Expression Trees
to the root.
Binary Search Trees
3 Each subtree is itself a binary search tree.
Tree concepts.38
Tree concepts
Valid Binary Search Trees
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.39
Tree concepts
Invalid Binary Search Trees
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.40
Tree concepts
Binary Search Tree (BST)
Dept. Computer
Science
Binary Trees
• BST is one of implementations for ordered list. Expression Trees
• In BST we can search quickly (as with binary Binary Search Trees
Tree concepts.41
Tree concepts
Binary Search Tree Traversals
Dept. Computer
Science
23
Expression Trees
Tree concepts.43
Tree concepts
Binary Search Tree Search
Dept. Computer
Science
Tree concepts.44
Tree concepts
Binary Search
Dept. Computer
Science
Recursive Search
1 Algorithm searchBST(val root <pointer>, val Basic Tree Concepts
Binary Trees
target <keyType>)
Expression Trees
2 Search a binary search tree for a given value.
Binary Search Trees
Tree concepts.45
Tree concepts
Binary Search
Dept. Computer
Science
Recursive Search
1 if root is null then
2 return null Basic Tree Concepts
3 end Binary Trees
Expression Trees
4 if target < root->data.key then Binary Search Trees
5 return searchBST(root->left, target)
6 else if target > root->data.key then
7 return searchBST(root->right, target)
8 else
9 return root
10 end
11 End searchBST
Tree concepts.46
Tree concepts
Binary Search
Dept. Computer
Science
Iterative Search
1 Algorithm iterativeSearchBST(val root Basic Tree Concepts
2 Search a binary search tree for a given value using Expression Trees
Tree concepts.47
Tree concepts
Binary Search
Dept. Computer
Science
Iterative Search
1 while (root is not NULL) AND (root->data.key != Basic Tree Concepts
target) do Binary Trees
2 if target < root->data.key then Expression Trees
4 else
5 root = root->right
6 end
7 end
8 return root
9 End iterativeSearchBST
Tree concepts.48
Tree concepts
Insert Node into BST
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.50
Tree concepts
Insert Node into BST: Iterative Insert
Dept. Computer
1 if root is null then Science
2 root = new
3 else
4 pWalk = root
5 while pWalk not null do Basic Tree Concepts
9 else
10 pWalk = pWalk->right
11 end
12 end
13 if new->data.key < parent->data.key then
14 parent->left = new
15 else
16 parent->right = new
17 end
18 end Tree concepts.51
Tree concepts
Insert Node into BST: Recursive Insert
Dept. Computer
Science
Tree concepts.52
Tree concepts
Insert Node into BST: Recursive Insert
Dept. Computer
Science
6 else
7 recursiveInsertBST(root->right, new)
8 end
9 end
10 Return
11 End recursiveInsertBST
Tree concepts.53
Tree concepts
Delete node from BST
Dept. Computer
Science
Binary Trees
Expression Trees
Binary Trees
Expression Trees
Binary Trees
Replace the deleted node by its predecessor or by its Binary Search Trees
Tree concepts.56
Tree concepts
Delete node from BST
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.57
Tree concepts
Delete node from BST
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.58
Tree concepts
Delete node from BST
Dept. Computer
Science
Expression Trees
3 Pre: root is pointer to tree containing data to be
Binary Search Trees
deleted
4 dltKey is key of node to be deleted
Tree concepts.59
Tree concepts
Delete node from BST
Dept. Computer
Science
Tree concepts.60
Tree concepts
Delete node from BST
Dept. Computer
Science
1 else
2 // Deleted node found – Test for leaf node Basic Tree Concepts
3 if root->left is null then
Binary Trees
4 dltPtr = root
Expression Trees
5 root = root->right
Binary Search Trees
6 recycle(dltPtr)
7 return true
8 else if root->right is null then
9 dltPtr = root
10 root = root->left
11 recycle(dltPtr)
12 return true
Tree concepts.61
Tree concepts
Delete node from BST
Dept. Computer
Science
1 else
2 // ...
3 else
Basic Tree Concepts
4 // Deleted node is not a leaf.
Binary Trees
5 // Find largest node on left subtree
Expression Trees
6 dltPtr = root->left
7 while dltPtr->right not null do Binary Search Trees
8 dltPtr = dltPtr->right
9 end
10 // Node found. Move data and delete leaf node
11 root->data = dltPtr->data
12 return deleteBST(root->left, dltPtr->data.key)
13 end
14 end
15 End deleteBST
Tree concepts.62
Tree concepts
Dept. Computer
Science
Binary Trees
Expression Trees
Tree concepts.63