Data Structures and Applications - Module-4
Data Structures and Applications - Module-4
• Introduction
• Terminology
• Binary Trees
• Properties of binary trees
• Array and Linked representations of binary tree
• Binary Tree Traversals
• Additional Binary Tree Operations
• Threaded Binary Trees
• Binary Search Trees
• Binary Search Trees operations
• Application of Trees-Evaluation of Expression
Introduction (1/8)
• A tree structure means that the data are organized so
that items of information are related by branches
• Examples:
Introduction (2/8)
B B
output A / B * C * D + E
:
ptr
L
V
R
Binary Tree Traversals (4/9)
• Preorder traversal (VLR) (recursive version)
output + * * / A B C D E
:
V
L
R
Binary Tree Traversals (5/9)
• Postorder traversal (LRV) (recursive version)
output A B / C * D * E +
:
L
R
V
Binary Tree Traversals (6/9)
• Iterative inorder traversal
• we use a stack to simulate recursion
5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+
L
V
output + * E * D / C A B
:
1 17 3 14 4 11 5
2 8
*+ E * D / C A B
FIFO
ptr
Additional Binary Tree Operations (1/7)
• Copying Binary Trees
• we can modify the postorder traversal algorithm only
slightly to copy the binary tree
similar as
Program 5.3
Additional Binary Tree Operations (2/7)
• Testing Equality
• Binary trees are equivalent if they have the same topology
and the information in corresponding nodes is identical
V L R
postorder traversal
data
value X3
X1 X3
X2 X1
Additional Binary Tree Operations (6/7)
• node structure
• For the purpose of our evaluation algorithm, we assume
each node has four fields:
TRUE
FALSE
FALSE
TRUE T
TRUE
F
T TRUE FALSE FALSE
TRUE
FALSE F T TRUE
Threaded Binary Trees (1/10)
• Threads
• Do you find any drawback of the above tree?
• Too many null pointers in current representation of binary
trees
n: number of nodes
number of non-null links: n-1
total links: 2n
null links: 2n-(n-1) = n+1
• Solution: replace these null pointers with some useful
“threads”
Threaded Binary Trees (2/10)
• Rules for constructing the threads
• If ptr->left_child is null,
replace it with a pointer to the node that would be visited
before ptr in an inorder traversal
• If ptr->right_child is null,
replace it with a pointer to the node that would be visited
after ptr in an inorder traversal
Threaded Binary Trees (3/10)
• A Threaded Binary Tree
root A
t: true thread
f: false child
dangling
f B f C
D t E t F G
dangling
inorder traversal:
H I
H D I B E A F C G
Threaded Binary Trees (4/10)
Inorder
Threaded Binary Trees (8/10)
• Inorder traversal of a threaded binary tree
void tinorder(threaded_pointer tree){
/* traverse the threaded binary tree inorder */
medium
smaller larger
Binary Search Trees (3/8)
• Search: Search(25) Search(76)
44
17 88
32 65 97
28 54 82
29 76
80
Binary Search Trees (4/8)
• Searching a
binary search
tree
O(h)
Binary Search Trees (5/8)
• Inserting into a binary search tree
An empty tree
Binary Search Trees (6/8)
• Deletion from a binary search tree
• Three cases should be considered
• case 1. leaf delete
• case 2.
one child delete and change the pointer to this child
• case 3. two child either the smallest element in the right
subtree or the largest element in the left subtree
Binary Search Trees (7/8)