0% found this document useful (0 votes)
154 views24 pages

Unit 6 - Trees

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views24 pages

Unit 6 - Trees

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Unit VI

Trees (16 Marks)


Tree:
1. A tree is a non-linear data structure.
2. It is a hierarchical collection of nodes where each node contains data and
information about successor nodes. A tree represents hierarchy.
3. There is no unique sequence to traverse all the elements of the tree.
4. For ex.
General terminology
Consider the following tree T,

1. Root – It is the mother node of the tree structure. This node is the only node
that does not have any parent. It is the first node in the hierarchical
arrangement.
In above given tree, A is root
2. Parent – It is the immediate predecessor of a node.
A is the parent of B and C.
B and C are children of A
3. Sibling- The child nodes of same parent are called siblings.
D and E are siblings.
4. Edge – The connecting line drawn from a node to its successor node is an edge.
5. Path – A sequence of consecutive edges is called a path.
6. Degree of a node – The maximum number of children that can exist for a node
is called degree/ outdegree of a node.
Degree of C is 2 and G is 1
7. Indegree of a node – The number of edges arriving at a node is called as
indegree of a node. The indegree of every node in a tree is 1.
8. Degree of tree – The maximum degree of nodes in a given tree is called the
degree of the tree.
Degree of tree T is 2.
9. Leaf nodes – Nodes with no children are called leaf nodes or terminal nodes.
D and H are leaf nodes
10. Internal nodes – Nodes other than leaf nodes are called branch nodes or
internal nodes.
B and G are internal nodes.
11. Level – It is the rank of the tree hierarchy. The whole tree structure is levelled.
The level of root node is always 0. The immediate children of root are at level
1 and so on.
Level of node F is 2
12. Height/ Depth of the tree – The highest number of nodes that is possible in a
way starting from the first node (root) to a leaf node is called the height of the
tree.
Height of tree T is 4.
13. Ancestor and Descendants – If there is a path from node n1 to n2 then n1 is
the ancestor of n2 and n2 is the descendant of n1.
A and B are ancestors of E and H.
I, is a descendant of A and C.

Types of Trees
1. General tree – A tree, T is defined as a finite set of one or more nodes such
that there is a node called root and the nodes other than the root form a
collection of disjoint trees T1, T2, T3,….,Tn. T1, T2are called as subtrees or
children of the root.
2. Binary Tree – An ordered tree is a tree in which the children of each node are
ordered. The binary tree is a type of ordered tree in which every node has
either no children, one child or two children.
In a binary tree the child node at the left is called the left child and that
at the right is called as the right child.
3. Binary Search Tree – In a binary search tree, constraints are placed on how
the data elements can be stored. All items in the left subtree of the root are
less than the value of the root and all items to its right are greater. If this
relationship holds in all the nodes in the tree, then the tree is called as a Binary
Search Tree.
This form of tree is used in applications where searching algorithms are
used. The binary search tree reduces the time complexity of the algorithm to
less than 50%.
General Tree Binary Tree
1. A general tree is a data structure in 1. A binary tree is a data structure in
which each node can have infinite which each node can have at most
number of children two children i.e. left and right
2. Root node has indegree= 0 and 2. Root node has indegree = 0 and
maximum outdegree = n maximum outdegree = 2
3. All other nodes have indegree = 1 3. All other nodes have indegree =1
and maximum outdegree = n and maximum outdegree = 2
4. Subtrees of general tree may not 4. Subtrees of binary trees are
be ordered ordered
5. 5.

Sequential (Array) Representation of Binary Trees


1. Consider a binary tree T, of height h. The sequential representation of this tree
will require an array, say tree, of size 2h-1, which is the maximum number of
nodes T can hold.
2. The root node of T is stored at tree[0]. If a node occupies tree[k], then its left
child is stored in tree[2k+1] and right child is stored in tree[2k+2]. And parent is
stored in tree[(k-1)/2].
3. NULL is used to denote an empty subtree. If tree[0] = NULL, then the tree is
empty.
4. With this type of representation direct access to any node is possible and finding
the parent or left and right child of a particular node is fast due to random
access. Also, insertions and deletions are easier.
5. Disadvantages:
a. Memory wastage is a major disadvantage in array representation. In case
of skewed trees, half of the array is unutilized.
b. In this type of representation, the height of the tree has to be fixed for
array size declaration. If it is larger then memory is wasted and if it is less
than the height of the tree, then some part of the tree cannot be
represented.

Linked List Representation of Binary Trees


1. In the linked list representation, each element of the binary tree is represented
by a tree node.
2. Each linked list node contains two links to the same structure – left and right.
3. The pointer root points to the root node of the tree. If tree is empty, then it is
denoted by root = NULL.
4. The left link points to the left subtree and the right link points to the right
subtree of the tree.
5. The structure of the tree node is,
Pointer to left Pointer to right
Data field
subtree subtree

It is given as,

struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
};
Operations on Binary Trees
1. Insertion
2. Deletion
3. Searching
4. Traversing

Insertion
1. For inserting a node in a binary tree, initially the data to be inserted is compared
with the data of the root node.
2. If the data is found to be less than or equal to the data of the root node then the
new node is to be inserted in the left subtree of the root or else it is inserted in
the right subtree of the root.
3. Again, the data is compared with the root node of the left or right subtree and
the procedure is repeated till the left or right subtree where the node to be
inserted is found to be empty. Finally, the new node is made the appropriate child
of this current node.
4. Consider the following tree,

5. To insert item 18 in the tree, we begin from root node.


Here, item and data at root node are compared and accordingly the left or right
link is considered for insertion
a. Initially, root data = 38. Since 18 < 38 , go to left child.
b. Next 14 < 18, go to right child.
c. Next, 23 > 18, go to left child.
Here, left child link is NULL. So, 18 is inserted as left child of 23.
Searching
1. To search any item in the binary tree, the item is compared with data at root
node. If there is a match search is successful.
2. If the item is less than data at root node then search is continued in left subtree
or searching proceeds in right subtree.
3. Again, item is compared with the subtree root nodes. And accordingly search
proceeds to the left or right child until item is found.
4. While searching if a leaf node is reached and item is not found then it is concluded
that it is not present in the tree.
5. Consider the following tree,

6. To search item 7 ,
a. We begin with root node. Since 7 > 5, go to right subtree.
b. Next, 7 < 8, go to left subtree.
c. Here, 7 = 7, item found in tree.
Deletion
1. In deletion the tree empty condition needs to be checked.
2. If the tree is nonempty then the given item has to be first searched in the tree. If
the node say, temp, is found then there are four possible conditions,
a. temp has two children
b. temp has no children.
c. temp has only left child.
d. temp has only right child
3. In case a, the inorder successor data is placed in node temp and the inorder
successor node is renamed as temp. So, the deletion process can further continue
as b, c or d.
4. In case of b, c ,d, the node temp is deleted from tree by setting the parent node
pointers.
Binary tree traversals
Traversing is the process of visiting every node in the tree exactly once. So the
complete traversal of the binary tree gives all the nodes of the tree in some linear
sequence.
There are three ways of traversing a binary tree T,
1. Inorder ( LDR) – The inorder traversal of the tree traverses the left subtree, visits
the root and then traverses the right subtree in inorder.
2. Preorder (DLR) – The preorder traversal of the tree visits the root first, then
traverses the left subtree in preorder and then the right subtree in preorder.
3. Postorder (LRD) – The postorder traversal of the tree traverses the left subtree in
postorder, the right subtree in postorder and then visits the root.

B C
B

Inorder traversal: BAC


Preorder traversal: A B C
Postorder traversal: B C A
Expression Trees
An expression tree can be constructed using the infix form of an arithmetic
expression.
The inorder traversal of an expression tree gives the infix form of the arithmetic
expression. Similarly, the preorder traversal gives the pefix form and postorder form
gives the postfix form of the expression.
Heap
1. A Heap is a complete binary tree data structure that satisfies the heap
property: for every node, the value of its children is greater than or equal to
its own value.
2. It either follows max heap or min heap property.
3. Max-Heap: The value of the root node must be the greatest among all its
descendant nodes and the same thing must be done for its left and right sub-
tree also.

4. Min-Heap: The value of the root node must be the smallest among all its
descendant nodes and the same thing must be done for its left and right sub-
tree also.

5. The minimum or maximum element is always at the root of the heap, allowing
constant-time access.
6. The heap data structure is used in Heap Sort, Priority Queues.
7. As the tree is complete binary, all levels are filled except possibly the last level.
And the last level is filled from left to right.
8. When we insert an item, we insert it at the last available slot and then
rearrange the nodes so that the heap property is maintained.
9. When we remove an item, we swap root with the last node to make sure either
the max or min item is removed. Then we rearrange the remaining nodes to
ensure heap property (max or min).
10. Operations on heap:
a) Heapify – Process to rearrange the heap in order to maintain heap-
property.
b) Find-max (or Find-min) – find a maximum item of a max-heap, or a
minimum item of a min-heap, respectively.
c) Insertion – Add a new item in the heap.
d) Deletion – Delete an item from the heap.
e) Extract Min-Max – Returning and deleting the maximum or minimum
element in max-heap and min-heap respectively.

You might also like