Unit 3
Unit 3
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.
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
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.
In array the node index starts from zero to maxsize -1. Here maxsize represents the maximum no of
nodes present in the tree.
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 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
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.
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.
PREORDER TRAVERSAL(NLR)
INORDER TRAVERSAL(LNR)
POSTORDER TRAVERSAL(LRN)
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 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
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.
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...
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:
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.
• 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.
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.
******