0% found this document useful (0 votes)
15 views18 pages

Trees, Binary Search Trees, Traversals, Ordered Map From STL

The document provides an overview of trees and binary search trees, explaining their structure, properties, and implementations. It details the time complexity of various operations and describes different types of tree traversals, including pre-order, in-order, and post-order. Additionally, it introduces ordered maps from the Standard Template Library (STL) and their use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views18 pages

Trees, Binary Search Trees, Traversals, Ordered Map From STL

The document provides an overview of trees and binary search trees, explaining their structure, properties, and implementations. It details the time complexity of various operations and describes different types of tree traversals, including pre-order, in-order, and post-order. Additionally, it introduces ordered maps from the Standard Template Library (STL) and their use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

5.

Trees, binary search


trees, traversals,
ordered map from
STL
1/18
5. Trees, binary search trees, traversals, ordered
map from STL

What is a tree?

 Widely used data structure that simulates a hierarchichal tree

structure, with parents and children representing the nodes

 Hierarchichaly organized set of data.

 Collection of nodes

2/18
5. Trees, binary search trees, traversals, ordered
map from STL

Example of a tree structure:

Depth of tree

0
Parent (Root node)

1 Children of the node (2)

Simple unordered tree

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

Running time complexity of a tree

Operation Time Complexity


Search O (n)

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

What is a binary search tree?

 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

Difference between a binary search tree and a binary tree

 Binary tree is the tree in which every node has no, one or utmost two children.

There is no condition or relationship between the values of the parent and


children nodes.

 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

Properties of a binary search tree

 The left subtree of a node contains only nodes with keys

lesser than the parent node’s key.

 The right subtree of a node contains only nodes with keys

greater than the parent node’s key.

 The left and right subtree each must also be a binary

search tree.

8/18
5. Trees, binary search trees, traversals, ordered
map from STL

Implementation of a binary search tree (1)

Examples of incorrect implemented binary search trees: only B is correct.

9/18
5. Trees, binary search trees, traversals, ordered
map from STL

Implementation of a binary search tree (2)

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

Running time complexity of a binary search tree

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

What is a tree traversal?

 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.

 Traversals are classified by the order in which nodes are visited.

12/18
5. Trees, binary search trees, traversals, ordered
map from STL

Types of tree traversals

 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

Examples of tree traversals

Pre-order Traversals In-order Traversals Post-order Traversals


Visit parent before children Visit child, parent, child Visit children before parent
Parent  left  right Left  parent  right Left  right  parent

14/18
5. Trees, binary search trees, traversals, ordered
map from STL

Running time complexity of tree traversals

Operation Time Complexity *


Pre-order traversal O (n)

In-order traversal O (n)

Post-order traversal O (n)

* 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.

• We implement it with “std::map”

• Interesting to use when we:


• Need ordered data

• Need to access the data in a sorted orde

16/19
Example

17/19
5. Trees, binary search trees, traversals, ordered
map from STL

Thank you!

18/18

You might also like