Trees: - Introduction - Representation of Trees - Binary Trees - Binary Tree Traversals - Binary Search Trees
Trees: - Introduction - Representation of Trees - Binary Trees - Binary Tree Traversals - Binary Search Trees
• Introduction
• Representation Of Trees
• Binary Trees
• Binary Tree Traversals
• Binary Search Trees
INTRODUCTION
• Thus the left subtree and the right subtree are distinguished
A A
B B
output: A / B* C * D + E
ptr
L
V
R
• Preorder traversal (VLR) (recursive version)
output: + * * / A B C D E
V
L
R
• Postorder traversal (LRV) (recursive version)
output: A B / C * D * E +
L
R
V
• Iterative inorder traversal
• we use a stack to simulate recursion
5 84 11
3 14
2 17
1
A B
/ *C D
* E
+
L
V
medium
smaller larger
• Searching a
binary search
tree
O(h)
• Inserting into a binary search tree
An empty tree
• 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
• Height of a binary search tree
• The height of a binary search tree with n elements can become as large
as n.
• It can be shown that when insertions and deletions are made at
random, the height of the binary search tree is O(log2n) on the average.
• Search trees with a worst-case height of O(log2n) are called balance
search trees