0% found this document useful (0 votes)
4 views

Trees in Data Structure

Trees in Data Structure, Tree Terminologies, Applications of trees, Depth First Search DFS, Breadth First Search BFS

Uploaded by

Muhammad Haroon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Trees in Data Structure

Trees in Data Structure, Tree Terminologies, Applications of trees, Depth First Search DFS, Breadth First Search BFS

Uploaded by

Muhammad Haroon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

DATA STRUCTURES AND ALGORITHMS

1
Trees
2 Tree - Definition
▪ Data structure such as Arrays, Stacks, Linked List and Queues are
linear data structure. Elements are arranged in linear manner i.e. one
after another.

▪ Tree is a non-linear data structure.

▪ Tree imposes a Hierarchical structure, on a collection of items.


Family Tree
3
root
Sub-trees

Banu Hashim
4 Tree
▪ Root node drawn at the top
▪ Left child of the root node (if any)
▪ Drawn below and to the left of the root node
▪ Right child of the root node (if any)
▪ Drawn below and to the right of the root node
▪ Directed edge (directed branch): arrow

B F K

C H D L X

Q G M I N P
5 Tree Terminologies
Root

Level 0 Internal Nodes

Level 1
Arc/Edge

Level 2

Level 3
Parent

Leaf Siblings
6 Tree Terminologies A

� Root B C

� It is the key node of a tree structure that does not have


parent. It is the first node in hierarchical arrangement.
� Node
� The node of a tree stores the data and its role is the same
as in the linked list. Nodes are connected by the means of
links with other nodes.
� Parent
� It is the immediate predecessor of a node. In the figure A
is the parent of B and C.
A
7 Tree Terminologies
B C
� Child
� All successor nodes are called child nodes. In the figure B and C
are the child nodes of A
� Sibling
� The child node of same parent are called sibling.
� Link / Edge
� An edge connects the two nodes. The line drawn from one node
to other node is called edge / link. Link is nothing but a pointer
to node in a tree structure.
� Leaf / Terminal Node
� This node is located at the end of the tree. It does not have any
child hence it is called leaf node.
8 Tree Terminologies
� Level
� Level is the rank of tree hierarchy. The whole tree structure is leveled. The level
of the root node is always at 0. the immediate children of root are at level 1 and
their children are at level 2 and so on.
� Depth
� The depth of a node n is the length of the path from the root to the node.
� The depth (or height) of a tree is the length of the path from the root to the
deepest node in the tree.

Root node

Interior nodes Height

Leaf nodes
9 Ancestors
� Ancestors of a node: parent, grandparent, grand-grandparent, etc.
� Descendant of a node: child, grandchild, grand-grandchild, etc.

A
Descendants of K?

B F K

C H D L X

Q G M I N P

Ancestors of Q?
10 Examples – familiarization with
notation
11 Tree Pseudocode:
12 Applications of trees
•Storing naturally hierarchical data: Trees are used to store the
data in the hierarchical structure. For example, the file system. The
file system stored on the disc drive, the file and folder are in the form
of the naturally hierarchical data and stored in the form of trees.

•Organize data: It is used to organize data for efficient insertion,


deletion and searching. For example, a binary tree has a logN time for
searching an element.
13 Applications of trees: Continue..

•Heap: It is also a tree data structure implemented using arrays. It is


used to implement priority queues.

•B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures
used to implement indexing in databases.

•Routing table: The tree data structure is also used to store the data in
routing tables in the routers.
14 Breadth-first search
� Breadth first search (BFS), as the name implies,
searches from the initial state breadth-wise.
� That is it searches all the states in the tree level
by level.
� Only after exploring all the states in one level it
will jump to the next level.
� Once the solution is found the search stops.
� The breadth first search is guaranteed to find the
solution if one exists.
15 Breadth-first search

� Expand shallowest unexpanded node


� Implementation:
16 Breadth-first search

� Expand shallowest unexpanded node


� Implementation:
17 Breadth-first search

� Expand shallowest unexpanded node


� Implementation:
18 Breadth-first search

� Expand shallowest unexpanded node


Breadth-first searching
19

� A breadth-first search (BFS)


A explores nodes nearest the root
before exploring nodes further
away
B C
� For example, after searching A,
then B, then C, the search
D E F G proceeds with D, E, F, G
� Node are explored in the order
ABCDEFGHIJKLMNO
H I J K PQ
� J will be found before N
L M N O P Q
20
Breadth-first search algorithm

Nodes are lining up to be visited in open.


closed keeps track of all the nodes visited already.
21 A trace of breadth-first algorithm

B is not the goal.


Put his children onto the queue.
| Put him in closed. He is done.
| |
Items between red bars are
| | siblings.
| | |
| | |
| | | |
goal is reached or open is empty.
22 Depth-first search

� Explore only one branch deeper till the solution is found or there
is no new state to explore and then start searching the adjacent
level.
� This technique is called depth first searh (DFS).
� By chance the solution exists in the first branch then depth first
search can find the solution quickly.
� If the solution exists in some other branches farther away, there
won't be much difference between depth first search and breadth
first search
23 Depth-first search

� Expand deepest unexpanded node

� Implementation:
24 Depth-first search

� Expand deepest unexpanded node

� Implementation:
25 Depth-first search

� Expand deepest unexpanded node

� Implementation:
26 Depth-first search

� Expand deepest unexpanded node

� Implementation:
27 Depth-first search

� Expand deepest unexpanded node

� Implementation:
28 Depth-first search

� Expand deepest unexpanded node

� Implementation:
29 Depth-first search

� Expand deepest unexpanded node

� Implementation:
30 Depth-first search

� Expand deepest unexpanded node

� Implementation:
31 Depth-first search

� Expand deepest unexpanded node

� Implementation:
32 Depth-first search

� Expand deepest unexpanded node

� Implementation:
33 Depth-first search

� Expand deepest unexpanded node

� Implementation:
34 Depth-first search

� Expand deepest unexpanded node

� Implementation:
Depth-first searching
35 � A depth-first search (DFS)
explores a path all the way to a
leaf before backtracking and
A exploring another path
� For example, after searching A,
then B, then D, the search
B C backtracks and tries another path
from B
D E F G � Node are explored in the order A
BDEHLMNIOPCFG
JKQ
H I J K
� N will be found before J
L M N O P Q
36 The depth-first search algorithm

This is the only difference between


depth-first and breadth-first.
A trace of depth-first algorithm
37

top of stack
38 Time and memory requirement in Depth-first

Depth Nodes Time (best case) Memory


0 1 1 millisec 100 bytes
2 20 0.02 sec 2 Kb
4 40 0.04 sec 4 Kb
6 10 * 6 0.06 sec 6 Kb
8 10 * 8 0.08 sec 8 Kb
10 10 *10 0.1 sec 10 Kb
12 10 * 12 0.12 sec 12 Kb
14 10 * 14 0.14 sec 14 Kb

Assume branching factor b=10; 1000 nodes explored/sec and 100 bytes/node
39 Search sequences of depth-first and breadth-first

Breadth first: A, B, C, …

Depth first: 1, 2, 3, …
Snap shot at iteration 6
40 frontier

visited
41 Comparing the ordering of search sequences

� Determine the order of nodes (states) to be examined


� Breadth-first search
� When a state is examined, all of its children are
examined, one after another
� Explore the search space in a level-by-level fashion
� Depth-first search
� When a state is examined, all of its children and their
descendants are examined before any of its siblings
� Go deeper into the search space where possible

You might also like