Lecture #10 - Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

TREE DATA STRUCTURE

Tree Data Structure


Tree represents the nodes connected by edges. We will discuss binary
tree or binary search tree specifically.

Binary Tree is a special datastructure used for data storage purposes.


A binary tree has a special condition that each node can have a
maximum of two children. A binary tree has the benefits of both an
ordered array and a linked list as search is as quick as in a sorted array
and insertion or deletion operation are as fast as in linked list.
ImportantTerms
Following are the important terms with respect to tree.  Subtree − Subtree represents the descendants of a
 Path − Path refers to the sequence of nodes along node.
the edges of a tree.  Visiting − Visiting refers to checking the value of a
 Root − The node at the top of the tree is called node when control is on the node.
root. There is only one root per tree and one path  Traversing − Traversing means passing through
from the root node to any node. nodes in a specific order.
 Parent − Any node except the root node has one  Levels − Level of a node represents the generation
edge upward to a node called parent. of a node. If the root node is at level 0, then its next
 Child − The node below a given node connected child node is at level 1, its grandchild is at level 2,
by its edge downward is called its child node. and so on.
 Leaf − The node which does not have any child  keys − Key represents a value of a node based on
node is called the leaf node. which a search operation is to be carried out for a
node.
Binary Search Tree Representation
Binary Search tree exhibits a special behavior. A node's left child must
have a value less than its parent's value and the node's right child must
have a value greater than its parent value.

We're going to implement tree using node object and connecting them
through references.
Tree Node
The code to write a tree node would be similar to what is given below.
It has a data part and references to its left and right child nodes.

In a tree, all nodes share common construct.


BST Basic Operations
The basic operations that can be performed on a binary search tree data
structure, are the following −

 Insert − Inserts an element in a tree/create a tree.


 Search − Searches an element in a tree.
 Preorder Traversal − Traverses a tree in a pre-order manner.
 Inorder Traversal − Traverses a tree in an in-order manner.
 Postorder Traversal − Traverses a tree in a post-order manner.

We shall learn creating (inserting into) a tree structure and searching a


data item in a tree in this chapter. We shall learn about tree traversing
methods in the coming chapter.
Insert Operation
 The very first insertion creates the tree. Afterwards, whenever an
element is to be inserted, first locate its proper location. Start
searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the
data. Otherwise, search for the empty location in the right subtree
and insert the data.

Search Operation
 Whenever an element is to be searched, start searching from the
root node, then if the data is less than the key value, search for the
element in the left subtree. Otherwise, search for the element in the
right subtree. Follow the same algorithm for each node.
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their
values too. Because, all nodes are connected via edges (links) we always
start from the root (head) node. That is, we cannot randomly access a
node in a tree.There are three ways which we use to traverse a tree −

 In-orderTraversal
 Pre-orderTraversal
 Post-orderTraversal

Generally, we traverse a tree to search or locate a given item or key in


the tree or to print all the values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root
and later the right sub-tree. We should always remember that every
node may represent a subtree itself.

If a binary tree is traversed in-order, the output will produce sorted


key values in an ascending order.
We start from A, and following in-order
traversal, we move to its left
subtree B. B is also traversed in-order.
The process goes on until all the nodes are
visited. The output of inorder traversal of
this tree will be −
D →B → E →A →F → C→ G
Pre-order Traversal
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.

We start from A, and following pre-order traversal, we first


visit A itself and then move to its left subtree B. B is also
traversed pre-order. The process goes on until all the nodes are
visited.The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Post-order Traversal
In this traversal method, the root node is visited last, hence the name.
First we traverse the left subtree, then the right subtree and finally the
root node.

We start from A, and following Post-order traversal, we first visit the


left subtree B. B is also traversed post-order. The process goes on until
all the nodes are visited. The output of post-order traversal of this tree
will be −
D →E → B →F → G→ C →A
Binary Search Tree
A Binary Search Tree (BST) is a tree in which all the nodes follow the
below-mentioned properties −
 The left sub-tree of a node has a key less than or equal to its
parent node's key.
 The right sub-tree of a node has a key greater than to its
parent node's key.

Thus, BST divides all its sub-trees into two segments; the left sub-tree
and the right sub-tree and can be defined as −
Representation
BST is a collection of nodes arranged in a way where they maintain BST
properties. Each node has a key and an associated value. While
searching, the desired key is compared to the keys in BST and if found,
the associated value is retrieved.

Following is a pictorial representation of BST −

We observe that the root node key (27) has all less-valued keys on the
left sub-tree and the higher valued keys on the right sub-tree.
Basic Operations
Following are the basic operations of a tree −
 Search − Searches an element in a tree.
 Insert − Inserts an element in a tree.
 Pre-orderTraversal − Traverses a tree in a pre-order manner.
 In-orderTraversal − Traverses a tree in an in-order manner.
 Post-order Traversal − Traverses a tree in a post-order manner.

Node
Define a node having some data, references to its left and right child
nodes.
Search Operation
Whenever an element is to be searched, start searching from the root
node. Then if the data is less than the key value, search for the element in
the left subtree. Otherwise, search for the element in the right subtree.
Follow the same algorithm for each node.

Insert Operation
Whenever an element is to be inserted, first locate its proper location.
Start searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the data.
Otherwise, search for the empty location in the right subtree and insert
the data.
AVL Trees
Named after their inventor Adelson, Velski & Landis, AVL
trees are height balancing binary search tree. AVL tree checks the
height of the left and the right sub-trees and assures that the difference
is not more than 1.This difference is called the Balance Factor.
What if the input to binary search tree comes in a sorted (ascending or
descending) manner? It will then look like this −

It is observed that BST's worst-case performance is closest to linear


search algorithms, that is Ο(n). In real-time data, we cannot predict
data pattern and their frequencies. So, a need arises to balance out the
existing BST.
Here we see that the first tree is balanced and the next two trees are not
balanced −

In the second tree, the left subtree of C has height 2 and the right subtree has
height 0, so the difference is 2. In the third tree, the right subtree of A has
height 2 and the left is missing, so it is 0, and the difference is 2 again. AVL
tree permits difference (balance factor) to be only 1.

If the difference in the height of left and right sub-trees is more than 1, the
tree is balanced using some rotation techniques.
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of
rotations −
 Left rotation
 Right rotation
 Left-Right rotation
 Right-Left rotation

The first two rotations are single rotations and the next two rotations
are double rotations. To have an unbalanced tree, we at least need a tree
of height 2.With this simple tree, let's understand them one by one.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right
subtree of the right subtree, then we perform a single left rotation −

In our example, node A has become unbalanced as a node is inserted in


the right subtree of A's right subtree. We perform the left rotation by
making Athe left-subtree of B.
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left
subtree of the left subtree.The tree then needs a right rotation.

As depicted, the unbalanced node becomes the right child of its left
child by performing a right rotation.
Left-Right Rotation
Double rotations are slightly complex version of already explained
versions of rotations. To understand them better, we should take note
of each action performed while rotation. Let's first check how to
perform Left-Right rotation. A left-right rotation is a combination of
left rotation followed by right rotation.
State Action State Action

A node has been inserted We shall now right-rotate


into the right subtree of the tree, making B the
the left subtree. This new root node of this
makes C an unbalanced subtree. C now becomes
node. These scenarios the right subtree of its own
cause AVL tree to perform left subtree.
left-right rotation.

We first perform the left


rotation on the left subtree The tree is now balanced.
of C. This makes A, the
left subtree of B.

Node C is still unbalanced,


however now, it is because
of the left-subtree of the
left-subtree.
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a
combination of right rotation followed by left rotation.
State Action
State Action

A node has been inserted into


Node A is still unbalanced
the left subtree of the right
because of the right subtree of
subtree. This makes A, an
its right subtree and requires a
unbalanced node with balance
left rotation.
factor 2.

A left rotation is performed by


making B the new root node of
First, we perform the right
the subtree. A becomes the left
rotation along C node,
subtree of its right subtree B.
making C the right subtree of its
own left subtree B.
Now, B becomes the right
subtree of A.

The tree is now balanced.


Spanning Tree
A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a spanning
tree does not have cycles and it cannot be disconnected..

By this definition, we can draw a conclusion that every connected and


undirected Graph G has at least one spanning tree. A disconnected
graph does not have any spanning tree, as it cannot be spanned to all its
vertices.
Spanning Tree

We found three spanning trees off one complete graph. A complete


undirected graph can have maximum nn-2 number of spanning trees,
where n is the number of nodes. In the above addressed example, n is
3, hence 33−2 = 3spanning trees are possible.
General Properties of Spanning Tree
We now understand that one graph can have more than one spanning
tree. Following are a few properties of the spanning tree connected to
graph G −
 A connected graph G can have more than one spanning tree.
 All possible spanning trees of graph G, have the same number
of edges and vertices.
 The spanning tree does not have any cycle (loops).
 Removing one edge from the spanning tree will make the
graph disconnected, i.e. the spanning tree is minimally
connected.
 Adding one edge to the spanning tree will create a circuit or
loop, i.e. the spanning tree is maximally acyclic.
Mathematical Properties of Spanning Tree
 Spanning tree has n-1 edges, where n is the number of nodes
(vertices).
 From a complete graph, by removing maximum e - n + 1 edges, we
can construct a spanning tree.
 A complete graph can have maximum nn-2 number of spanning
trees.

Thus, we can conclude that spanning trees are a subset of connected


Graph G and disconnected graphs do not have spanning tree.
Application of Spanning Tree
Spanning tree is basically used to find a minimum path to connect all
nodes in a graph. Common application of spanning trees are −
 Civil Network Planning
 Computer Network Routing Protocol
 Cluster Analysis

Let us understand this through a small example. Consider, city network


as a huge graph and now plans to deploy telephone lines in such a way
that in minimum lines we can connect to all city nodes. This is where
the spanning tree comes into picture.
Minimum Spanning Tree (MST)
In a weighted graph, a minimum spanning tree is a spanning tree that has
minimum weight than all other spanning trees of the same graph. In real-
world situations, this weight can be measured as distance, congestion,
traffic load or any arbitrary value denoted to the edges.

Minimum Spanning-Tree Algorithm


We shall learn about two most important spanning tree algorithms
here −
 Kruskal's Algorithm
 Prim's Algorithm

Both are greedy algorithms.


Heap Data Structures
Heap is a special case of balanced binary tree data structure where the
root-node key is compared with its children and arranged accordingly.
If α has child node β then −
key(α) ≥ key(β)

As the value of parent is greater than that of child, this property


generates Max Heap. Based on this criteria, a heap can be of two
types −

1. Min-Heap
2. Max-Heap
Min-Heap − Where the value Max-Heap − Where the value
of the root node is less than or of the root node is greater than
equal to either of its children. or equal to either of its
children.

Both trees are constructed using the same input and order of arrival.
Max Heap Construction Algorithm
We shall use the same example to demonstrate how a Max Heap is created.
The procedure to create Min Heap is similar but we go for min values instead
of max values.
We are going to derive an algorithm for max heap by inserting one element
at a time. At any point of time, heap must maintain its property. While
insertion, we also assume that we are inserting a node in an already heapified
tree.

Note − In Min Heap construction algorithm, we expect the value of the


parent node to be less than that of the child node.
Max Heap Deletion Algorithm
Let us derive an algorithm to delete from max heap. Deletion in Max
(or Min) Heap always happens at the root to remove the Maximum (or
minimum) value.

You might also like