Unit 6 - Trees
Unit 6 - Trees
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.
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,
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
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.