Trees in Data Structure
Trees in Data Structure
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.
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 1
Arc/Edge
Level 2
Level 3
Parent
Leaf Siblings
6 Tree Terminologies A
� Root B C
Root node
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.
•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
� 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
� Implementation:
24 Depth-first search
� Implementation:
25 Depth-first search
� Implementation:
26 Depth-first search
� Implementation:
27 Depth-first search
� Implementation:
28 Depth-first search
� Implementation:
29 Depth-first search
� Implementation:
30 Depth-first search
� Implementation:
31 Depth-first search
� Implementation:
32 Depth-first search
� Implementation:
33 Depth-first search
� Implementation:
34 Depth-first search
� 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
top of stack
38 Time and memory requirement in Depth-first
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