You are on page 1of 9

Application of Trees

Why Tree?
Unlike Array and Linked List, which are linear data structures, tree is hierarchical (or non-
linear) data structure.
1. One reason to use trees might be because you want to store information that naturally forms
a hierarchy. For example, the file system on a computer.
2. If we organize keys in form of a tree (with some ordering e.g., BST), we can search for a
given key in moderate time (quicker than Linked List and slower than arrays).
Self-balancing search trees like AVL and Red-Black trees guarantee an upper bound of
O(Logn) for search
3. We can insert/delete keys in moderate time (quicker than Arrays and slower than Unordered
Linked Lists). Self-balancing search trees like AVL and Red-Black trees guarantee an upper
bound of O(Logn) for insertion/deletion.
4. Like Linked Lists and unlike Arrays, Pointer implementation of trees don’t have an upper
limit on number of nodes as nodes are linked using pointers.
Other Applications :
 Store hierarchical data, like folder structure, organization structure, XML/HTML data.
 Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also allows
finding closest item
 Heap is a tree data structure which is implemented using arrays and used to implement priority
queues.
 B-Tree and B+ Tree : They are used to implement indexing in databases.
 Syntax Tree: Used in Compilers.
 K-D Tree: A space partitioning tree used to organize points in K dimensional space.
 Trie : Used to implement dictionaries with prefix lookup.
 Suffix Tree : For quick pattern searching in a fixed text.
 Spanning Trees and shortest path trees are used in routers and bridges respectively in computer
networks
 As a workflow for compositing digital images for visual effects
Graphs : Traversal mehods
Define a Graph
 In computer science, a graph is an abstract data type that is meant to implement the
undirected graph and directed graph concepts from the field of graph theory within
mathematics.
 A graph data structure consists of a finite (and possibly mutable) set of vertices (also
called nodes or points), together with a set of unordered pairs of these vertices for an
undirected graph or a set of ordered pairs for a directed graph. These pairs are known
as edges (also called links or lines), and for a directed graph are also known as edges but
also sometimes arrows or arcs. The vertices may be part of the graph structure, or may be
external entities represented by integer indices or references.
 A graph data structure may also associate to each edge some edge value, such as a
symbolic label or a numeric attribute (cost, capacity, length, etc.).
What is Graph traversals?
A graph traversal is a commonly used methodology for
locating the vertex position in the graph. It is an advanced
search algorithm that can analyze the graph with speed and
precision along with marking the sequence of the visited
vertices. This process enables you to quickly visit each node
in a graph without being locked in an infinite loop.
BFS(Breadth First search) Traversal
In this traversal a tree is traversed in the order of level of nodes. At a particular level, nodes are traversed from
left to right. First visit the root node, then go to the first level nodes and visit all nodes from left to right. Then
go to the second level and visit all nodes from left to right and so on untilall nodes have been visited.
The Algorithm is stated as, “ Visit all successors of a visited node before visiting any successors of any of
those successors”. If we visit the following tree, we get the sequence of node as SABCDGHEFIK.
Depth First Search (DFS) Traversal
A depth First Search Traversal is Similar to Preorder Traversal of a tree where we check the root of th tree,
then left node, then right node.
Here, the DFS traversal will be
SABDECGHIKJ

You might also like