Trees, Binary Search Trees, Traversals, Ordered Map From STL
Trees, Binary Search Trees, Traversals, Ordered Map From STL
What is a tree?
Collection of nodes
2/18
5. Trees, binary search trees, traversals, ordered
map from STL
Depth of tree
0
Parent (Root node)
3/18
5. Trees, binary search trees, traversals, ordered
map from STL
Implementation
4/18
5. Trees, binary search trees, traversals, ordered
map from STL
Insertion O (n)
Deletion O (n)
* n = number of nodes in the tree; because you traverse each node once.
5/18
5. Trees, binary search trees, traversals, ordered
map from STL
A tree data structure in which each node has at most (maximum) two
children; which are referred to as the left child and the right child.
Also called ordered or sorted binary tree, it keeps its values in sorted order,
so that the binary search principle can be applied to lookup for elements.
6/18
5. Trees, binary search trees, traversals, ordered
map from STL
Binary tree is the tree in which every node has no, one or utmost two children.
Binary search tree (which also inherits the properties of a binary tree), the node
with value smaller than the parent node must become the left child and the
node with value greater than or equal to the parent node must become the
right child.
7/18
5. Trees, binary search trees, traversals, ordered
map from STL
search tree.
8/18
5. Trees, binary search trees, traversals, ordered
map from STL
9/18
5. Trees, binary search trees, traversals, ordered
map from STL
We want to insert the key “40” A new key is always inserted at leaf. We start searching
a key from root till we hit a leaf node. Once a leaf node is found, the new node is added
as a child of the leaf node.
10/18
5. Trees, binary search trees, traversals, ordered
map from STL
The worst case time complexity of search and insert operations is O (h) where h is the
height of the Binary Search Tree.
In the worst case, we may have to travel from root to the deepest leaf node. The height of a
skewed tree may become n and the time complexity of search and insert operation may
become O (n).
11/18
5. Trees, binary search trees, traversals, ordered
map from STL
Also known as tree search, it refers to the process of visiting each node
in a tree data structure, exactly once.
Traversing a tree involves iterating over all nodes in some manner; that
is checking and/or updating each node.
12/18
5. Trees, binary search trees, traversals, ordered
map from STL
Pre-order traversals Each node is processed before any nodes in its subtrees.
(1st parents, 2nd children)
In-order traversal Each node is processed after all nodes in its left subtree but
before any nodes in its right subtree. (1st left children, 2nd parents, 3rd right
children)
Post-order traversal Each node is processed after all nodes in both its
subtrees. (1st children, 2nd parents)
13/18
5. Trees, binary search trees, traversals, ordered
map from STL
14/18
5. Trees, binary search trees, traversals, ordered
map from STL
* n = number of nodes in the binary tree; because you traverse each node once.
15/18
Ordered map from STL
• Maps are associative containers that store elements in a mapped fashion. Each
element has a key value and a mapped value. No two mapped values can have
same key values.
16/19
Example
17/19
5. Trees, binary search trees, traversals, ordered
map from STL
Thank you!
18/18