Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3

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

UNIT-3

TREE
Def:- A non linear data structure where the elements are arranged in a hierarchical manner and in a
sorted sequence.

A tree is defined as a finite set of zero or more nodes such that One special designated node called Root

The remaining nodes are partitioned into a collection of sub tree each of which is a tree

COMPONENTS OF TREE

NODE:- Each data item in a tree is called a Node. It has a name it carries information and links to other
nodes.

ROOT:-The node which has no parent is called Root..

EDGE:- It is a connection line between two nodes. if a tree has N nodes then it has N-1 edges. E.g. AB,
BE

LEVEL:-Level of node is the number given to each successive stage of nodes. Root is at level 0, its
children at level 1 and their children at level 2 and so on.
DEGREE OF A NODE:-It is the no of sub-trees of a node in a given tree. e.g. degree of node A is 3, degree
of node C is 1

TERMINAL NODES:- Nodes having no children are called Leaf or terminal nodes.e.g. E,J,G,H,K,L,M

NON-TERMINAL NODES:-Node which has children nodes then it is called nonterminal node. e.g. B, C ,D

PATH:- It is a sequence of consecutive edges from the source node to the destination node. In the
figure, path between A and J are - AB ,BF, FJ .There is exactly one path between root to other node if
more path then it is a graph ,not tree.

DEPTH or HEIGHT:-It is the maximum level of any node in a given Tree. In the above tree Root node A
has maximum level. height = depth = the level of the leaf in the longest path from the root + 1

FOREST:-It is a set of disjoint trees in a given tree. If we remove the root node then it becomes a forest .
In the above figure there is a forest with 3 trees.

BINARY TREE

A binary tree T is a finite set of nodes such that,

1. It is either empty ( null tree or empty tree)

2. It consists of a node called ROOT with two disjoint binary trees called left subtree(T1) and right
subtree (T2). T1 is the left successor and T2 is the right successor.

3. A binary tree can’t have more than 2 children. Any node must have 0,1 or 2 successor.

REPRESENTATION OF BINARY TREE IN MEMORY

A binary tree is implemented in memory in two ways:-

1. Array representation 2.Linked representation


ARRAY REPRESENTATION

An array is used to store the node of a binary tree.

In array the node index starts from zero to maxsize -1. Here maxsize represents the maximum no of
nodes present in the tree.

So the array representation of this binary tree as follows:-

LINKED REPRESENTATION/DYNAMIC REPRESENTATION

The binary tree is represented by linked lists. Here every node of tree act as a node of linked list.Each
node consists of 3 parts:-

Data field , left child , right child

Data field holds the value . Left child contains the address of its left node and the right child contains the
address of right node.
STRICTLY BINARY TREE

If every non terminal node in a binary tree consists of non empty left subtree and right subtree then
such a tree is called strictly Binary tree.

In the tree all the non- terminal nodes such as B and E having non empty left and right subtree

COMPLETE BINARY TREE

In a complete binary tree exactly one node at level 0,two node at level 1 and 4 node at level 2 and so on.
All the nodes have both children except last node.

EXTENDED BINARY TREE

A binary tree is called extended or 2 -tree if each node N has either zero or 2 children. the nodes with 2
children are called internal nodes and zero children are called external nodes. In any binary tree most of
the nodes contains one children so we can add one or two children to extend it. Circles are used for
internal nodes and squares for external nodes.

TRAVERSAL OPERATION IN BINARY TREE


Traversal is the process of visiting each node exactly once in a systematic manner.

We are using only 3 standard traversal they are :

1. NLR – Preorder traversal


2. LNR- In order traversal

3. LRN -Post order Traversal

Tree Traversal Can be performed in two different ways :-

1. Recursive Traversal or Recursive Algorithm

2. Non Recursive Traversal or Non Recursive algorithm

Recursive Traversal & Recursive Algorithms

PREORDER TRAVERSAL(NLR)

1. Visit the Root(N)

2. Traverse the left subtree of root in preorder(L)

3. Traverse the right subtree of root in Preorder(R)

INORDER TRAVERSAL(LNR)

1. Traverse the left subtree of root in inorder(L)

2. Visit the root(N)

3. Traverse the right subtree of root in inorder(R)

POSTORDER TRAVERSAL(LRN)

1. Traverse the left subtree of root in post order(L)

2. Traverse the right subtree of root in post order(R)

3. Visit the root (N)


BINARY SEARCH TREE
Def:-A Binary Search Tree is a binary tree which satisfies following properties:

1. The value of the key of the left sub-tree is less than the value of its parent (root) node's key.

2. The value of the key of the right sub-tree is greater than or equal to the value of its parent (root)
node's key.
AVL Tree
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary
search tree but it is a balanced tree. A binary tree is said to be balanced if, the difference
between the heights of left and right subtrees of every node in the tree is either -1, 0 or +1. In
other words, a binary tree is said to be balanced if the height of left and right children of every
node differ by either -1, 0 or +1. In an AVL tree, every node maintains an extra information
known as balance factor. The AVL tree was introduced in the year 1962 by G.M. Adelson-
Velsky and E.M. Landis.

An AVL tree is defined as follows...

An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every
node is either -1, 0 or +1.
Balance factor of a node is the difference between the heights of the left and right subtrees of
that node. The balance factor of a node is calculated either height of left subtree - height of
right subtree (OR) height of right subtree - height of left subtree. In the following
explanation, we calculate as follows...
Balance factor = height Of Left Subtree – height Of Right Subtree

Example of AVL Tree

The above tree is a binary search tree and every node is satisfying balance factor condition. So
this tree is said to be an AVL tree.
Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.

AVL Tree Rotations


In AVL tree, after performing operations like insertion and deletion we need to check the balance factor of every
node in the tree. If every node satisfies the balance factor condition then we conclude the operation otherwise we
must make it balanced. Whenever the tree becomes imbalanced due to any operation we use rotation operations to
make the tree balanced.
Rotation operations are used to make the tree balanced.

Rotation is the process of moving nodes either to left or to right to make the tree balanced.
Single Left Rotation (LL Rotation)
In LL Rotation, every node moves one position to left from the current position. To understand LL Rotation, let us
consider the following insertion operation in AVL Tree...

Single Right Rotation (RR Rotation)


In RR Rotation, every node moves one position to right from the current position. To understand RR Rotation, let us
consider the following insertion operation in AVL Tree...
Left Right Rotation (LR Rotation)
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation, at first, every
node moves one position to the left and one position to right from the current position. To understand LR Rotation, let
us consider the following insertion operation in AVL Tree...

Right Left Rotation (RL Rotation)


The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at first every node
moves one position to right and one position to left from the current position. To understand RL Rotation, let us
consider the following insertion operation in AVL Tree...

Operations on an AVL Tree


The following operations are performed on AVL tree...

1. Search
2. Insertion
3. Deletion
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
M-way Trees
An M-way(multi-way) tree is a tree that has the following properties:

• Each node in the tree can have at most m children.


• Nodes in the tree have at most (m-1) key fields and pointers(references) to the children.

Consider the pictorial representation shown below of an M-way tree.

The above image shows a 4-way tree, where each node can have at most 3(4-1) key fields and at
most 4 children. It is also a 4-way search tree.

M-way Search Trees


An M-way search tree is a more constrained m-way tree, and these constrain mainly apply to the key
fields and the values in them. The constraints on an M-way tree that makes it an M-way search tree are:

• Each node in the tree can associate with m children and m-1 key fields.
• The keys in any node of the tree are arranged in a sorted order(ascending).
• The keys in the first K children are less than the Kth key of this node.
• The keys in the last (m-K) children are higher than the Kth key.

Consider the pictorial representation shown below of an M-way search tree:


M-way search trees have the same advantage over the M-way trees, which is making the search and
update operations much more efficient. Though, they can become unbalanced which in turn leaves us to
the same issue of searching for a key in a skewed tree which is not much of an advantage.

APPLICATIONS OF ALL TREES

1. Store hierarchical data, like folder structure, organization structure, XML/HTML data.
2. Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also
allows finding closest item
3. Heap is a tree data structure which is implemented using arrays and used to implement priority
queues.
4. B-Tree and B+ Tree : They are used to implement indexing in databases.
5. Syntax Tree: Scanning, parsing , generation of code and evaluation of arithmetic expressions
in Compiler design.
6. K-D Tree: A space partitioning tree used to organize points in K dimensional space.
7. Trie : Used to implement dictionaries with prefix lookup.
8. Suffix Tree : For quick pattern searching in a fixed text.
9. Spanning Trees and shortest path trees are used in routers and bridges respectively in
computer networks
10. As a workflow for compositing digital images for visual effects.
11. Decision trees.
12. Organization chart of a large organization.
13. In XML parser.
14. Machine learning algorithm.
15. For indexing in database.
16. IN server like DNS (Domain Name Server)
17. In Computer Graphics.
18. To evaluate an expression.
19. In chess game to store defense moves of player.
20. In java virtual machine.
21. the tree data structure is also used to store the data in routing tables in the routers.

******

You might also like