Unit 5 2
Unit 5 2
Trees
5.1 Non Linear Data Structure: Tree & Graph
Tree: A tree is defined as a finite set of one or more nodes such that
There is a special node called the root node R.
The remaining nodes are divided into n > 0 disjoint sets T1, T2,.,.,. TN, where each of
These sets are tree. T1, T2…., T n is called the sub tree of the root.
Graph: A graph is a set of vertices/nodes and edges. A tree is a set of nodes and edges. In the graph,
there is no unique node which is known as root. In a tree, there is a unique node which is known as root.
4. Level number: A level is the number of parent nodes corresponding to a given a node of the tree. It
is basically the number of ancestors from that node until the root node. So, for the root node (topmost
node), it's level is 0, since it has no parents.
8. Root Node: The node at the top of the tree is called root. In a directed tree, a node which has
indegree 0 is called the root node.
9. Leaf Node: A leaf node is any node that has two empty children. An internal node is any node that
has at least one non-empty child.
10. Directed Edge: An edge of directed tree is called as directed edge.
11. Path: A sequence of connecting edges from one node to another node is called Path.
12. Depth: The length of the path from Root Node to particular Node V is called depth of node V. In a
tree, Maximum level no is called depth of the tree
Binary Tree: A tree is called binary tree if each and every node has 0,1 or 2 branch(Out degree 0,1 or
2)
Strictly binary tree: A tree is called Strictly binary tree if each and every node has 0 or 2
branch(Out degree 0 or 2)
Complete Binary Tree: A binary tree is called Complete binary tree , if all level except last havethe
maximum number of possible nodes.(2levelno)
Similar binary trees: If two binary trees are similar in structure then they are said to be similar
binary trees. Consider following figure:
Copies of binary trees: If two binary trees are similar in structure and their corresponding nodes
having same value then they are said to be copies of binary trees.
General Tree: A Tree in which each node having either 0 or more child nodes is called generaltree.
Example:
Example:
General tree is given in the figure.
Step 1: Root Node of General tree becomes the Root node of binary tree.
Step 2: Now Root Node (A) has three child Nodes (B, H, E) in general tree. The leftmost node
(B) of the root node (A) in the general tree becomes the left most node of the root node
(A) in binary tree.
Step 3: Now Node H becomes the right node of B and Node E becomes the right node of H.
Step 4: Now Node B has only one left child node, which is C in general tree. So NodeC
becomes left child of Node B in binary tree.
Step 5: Now Node E has two child nodes (F, G). The leftmost node (F) of the node (E) in the
general tree becomes the left most node of the node E in the binary tree.
Step 2: Now we have to insert 68. First we compare 68 with the root node which is 45.
Since the value of 68 is greater then 45 so it is inserted to the right of the root node.
Step 3: Now we have to insert 35. First we compare 35 with the root node which is 45.
Since the value of 35 is less then 45 so it is inserted to the left of the root node.
There are three possibilities when we want to delete an element from binary search tree.
(1) If a node to be deleted has empty left and right sub tree then a node is deleted directly.
(2) If a node to be deleted has only one left sub tree or right sub tree then the sub tree of the
deleted node is linked directly with the parent of the deleted node.
(3)If a node to be deleted has left and right sub tree then we have to do following steps:
(a) Find next maximum (in order successor) of the deleted node.
(b) Append the right sub tree of the in order successor to its grandparent.
(c) Replace the node to be deleted with its next maximum (in order successor).
(1) If the key to be searched having value less than root node then we search the key in left subtree.
(2) If the key to be searched having value greater then root node then we search the key in right sub
tree.
Step 1:
Exit
Step 2:
If X=ROOT->INFO then
Return (ROOT)
Step 3:
Else
Traversal is the method of processing every nodes in the tree exactly once in a systematic
manner.
Types of Traversal:
There are three different types of tree traversal.
1. Preorder Traversal
2. In order Traversal
3. Post order Traversal
1. Preorder Traversal
The Preorder traversal follows the three steps:
1. Process the root node. (Vertices or Node)
PREORDER(T)
If T == NULL then
Write (DATA(T))
Step 5:[Finished]
Exit
2. Inorder Traversal
INORDER (T)
POSTORDER (T)
Syntax Analysis
It deals with the use of grammars in syntax analysis or parsing.