0% found this document useful (0 votes)
4 views

Unit 6 - Trees

The document provides an overview of tree data structures, defining key concepts such as root nodes, sub-trees, leaf nodes, and various types of trees including binary and binary search trees. It explains the properties of binary search trees and their advantages in searching efficiency, as well as traversal methods like pre-order, in-order, and post-order. Additionally, it highlights the complexity of general trees and the benefits of converting them into binary trees for easier manipulation.

Uploaded by

ruahabhjadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 6 - Trees

The document provides an overview of tree data structures, defining key concepts such as root nodes, sub-trees, leaf nodes, and various types of trees including binary and binary search trees. It explains the properties of binary search trees and their advantages in searching efficiency, as well as traversal methods like pre-order, in-order, and post-order. Additionally, it highlights the complexity of general trees and the benefits of converting them into binary trees for easier manipulation.

Uploaded by

ruahabhjadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Trees

INTRODUCTION OF TREES
• A tree is recursively defined as a set Root node
of one or more nodes where one A
node is designated as the root of the T1 T3
tree and all the remaining nodes can T2
be partitioned into non-empty sets B C D
each of which is a sub-tree of the
root
E F G H I
• Figure shows a tree where node A is
the root node; nodes B, C, and D are
children of the root node and form J K
sub-trees of the tree rooted at node
A.
Basic Terminology
• Root node The root node R is the topmost node in the tree. If R = NULL, then it
means the tree is empty.
• Sub-trees If the root node R is not NULL, then the trees T1 , T2 , and T3 are called
the sub-trees of R.
• Leaf node A node that has no children is called the leaf node or the terminal node.
• Path A sequence of consecutive edges is called a path. For example, in Fig. 9.1, the
path from the root node A to node I is given as: A, D, and I
• Ancestor node An ancestor of a node is any predecessor node on the path from root
to that node. The root node does not have any ancestors. In the tree given in Fig.
nodes A, C, and G are the ancestors of node K.
• Descendant node A descendant node is any successor node on any path from the
node to a leaf node. Leaf nodes do not have any descendants. In the tree given in
Fig. nodes C, G, J, and K are the descendants of node A.
continue
• Level number Every node in the tree is assigned a level number in such a
way that the root node is at level 0, children of the root node are at level
number 1. Thus, every node is at one level higher than its parent. So, all
child nodes have a level number given by parent’s level number + 1.
• Degree Of Node Degree of a node is equal to the number of children that a
node has. The degree of a leaf node is zero.
• Degree Of Tree In a tree data structure, the degree of a tree is the highest
degree of any node in the tree.
• In-degree In-degree of a node is the number of edges arriving at that node
• Out-degree Out-degree of a node is the number of edges leaving that node.
• Height of tree –The height of a tree is the number of edges on the longest
downward path between the root and a leaf.
TYPES OF TREES
• General trees
• Binary trees
• Binary search trees
• Expression trees
• Heap Tree
General trees
• General trees are data structures that store elements hierarchically.
• The top node of a tree is the root node and each node, except the root, has a parent.
• A node in a general tree (except the leaf nodes) may have zero or more sub-trees. General
trees which have 3 sub-trees per node are called ternary trees. However, the number of
sub-trees for any node may be variable. For example, a node can have 1 sub-tree, whereas
some other node can have 3 sub-trees.
• Although general trees can be represented as ADTs, there is always a problem when
another sub-tree is added to a node that already has the maximum number of sub-trees
attached to it.
• Even the algorithms for searching, traversing, adding, and deleting nodes become much
more complex as there are not just two possibilities for any node but multiple
possibilities.
• To overcome the complexities of a general tree, it may be represented as a graph data
structure thereby losing many of the advantages of the tree processes.
• Therefore, a better option is to convert general trees into binary trees.
Binary trees
• A binary tree is a data structure that is defined as a collection of elements
called nodes.
• In a binary tree, the topmost element is called the root node, and each node
has 0, 1, or at the most 2 children.
• A node that has zero children is called a leaf node or a terminal node. Every
node contains a data element, a left pointer which points to the left child,
and a right pointer which points to the right child.
• The root element is pointed by a 'root' pointer. If root = NULL, then it means
the tree is empty
Binary trees
Root node
• In the figure, R is the root node and the two
trees T1 and T2 are called the left and right 1
sub-trees of R.
• T1 is said to be the left successor of R. T1 2 T2 3

• Likewise, T2 is called the right successor of R.


• Note that the left sub-tree of the root node
consists of the nodes: 2, 4, 5, 8, and 9. 4 5 6 7

• Similarly, the right sub-tree of the root node


consists of nodes: 3, 6, 7, 10, 11, and 12.
• In the tree, root node 1 has two successors: 2 8 9 10 11 12

and 3. Node 2 has two successor nodes: 4 and 5.


Node 4 has two successors: 8 and 9. Node 5 has A binary tree is recursive by definition as every node
in the tree contains a left sub-tree and a right sub-tree.
no successor. Node 3 has two successor nodes: 6
Even the terminal nodes contain an empty left sub-tree
and 7. Node 6 has two successors: 10 and 11. and an empty right sub-tree. In Fig. nodes 5, 8, 9, 10, 11,
Finally, node 7 has only one successor: 12. and 12 have no successors and thus said to have empty
sub-trees.
Levels in Binary trees & terminologies
Parent If N is any node in T that has left successor
Root node
S1 and right successor S2, then N is called the
parent of S1 and S2. Correspondingly, S1 and S2 (level 0)
1
are called the left child and the right child of N.
Every node other than the root node has a (level 1) 2 3
parent.
Level number Every node in the binary tree is
assigned a level number. The root node is defined
to be at level 0. (level 2)
4 5 6 7

The left and the right child of the root node have
a level number 1.Similarly, every node is at one
level higher than its parents. So all child nodes
are defined to have level number as parent's level (level 3) 8 9 10 11 12

number + 1.
Sibling All nodes that are at the same level and
Degree of a node It is equal to the number of share the same parent are called siblings
children that a node has. The degree of a leaf
node is zero. (brothers). For example, nodes 2 and 3; nodes
4 and 5; nodes 6 and 7; nodes 8 and 9; and
For example, in the tree, degree of node 4 is 2, nodes 10 and 11 are siblings.
continue.. • Similar Binary Trees
• Leaf node A node that has no children is called a leaf F
node or a terminal node. The leaf nodes in the tree A
are: 8, 9, 5, 10, 11, and 12. G H
B C
• Similar binary trees Two binary trees T and T' are said
to be similar if both these trees have the same
D I
structure.
• Copies Two binary trees T and T' are said to be copies J
E
if they have similar structure and if they have same • T' is a copy of T
content at the corresponding nodes. •
Tree T Tree T'
• Edge It is the line connecting a node N to any of its
successors. A binary tree of n nodes has exactly n – 1
edges because every node except the root node is A
connected to its parent via an edge A

• Path A sequence of consecutive edges. B C B C


• Depth The depth of a node N is given as the length of
the path from the root R to the node N. The depth of In-degree/out-degree
D of a nodeDIt is the E
E
the root node is zero. number of edges arriving at a node. The root node is
• Height of a tree It is the total number of nodes on the the only node that has an in-degree equal to zero.
path from the root node to the deepest node in the Similarly, out-degree of a node is the number of edges
tree. A tree with only a root node has a height of 1. leaving that node.
Binary Search Trees
• . A binary search tree, also known as an ordered binary tree, is a
variant of binary trees in which the nodes are arranged in an order.
• In a binary search tree, all the nodes in the left sub-tree have a value
less than that of the root node.
• Correspondingly, all the nodes in the right sub-tree have a value
either equal to or greater than the root node
• The same rule is applicable to every sub-tree in the tree.
Binary Search Trees
• The root node is 39. The left sub-tree
of the root node consists of nodes 9,
10, 18, 19, 21, 27, 28, 29, and 36.
• All these nodes have smaller values
than the root node. The right sub-tree
of the root node consists of nodes 40,
45, 54, 59, 60, and 65.
• Recursively, each of the sub-trees also
obeys the binary search tree constraint.
• For example, in the left sub-tree of the
root node, 27 is the root and all
elements in its left sub-tree (9, 10, 18,
19, 21) are smaller than 27, while all
nodes in its right sub-tree (28, 29, and
36) are greater than the root node’s
value
Binary Search Trees
• Since the nodes in a binary search tree are ordered, the time needed
to search an element in the tree is greatly reduced.
• Whenever we search for an element, we do not need to traverse the
entire tree. At every node, we get a hint regarding which sub-tree to
search in.
• For example, in the given tree, if we have to search for 29, then we
know that we have to scan only the left sub-tree. If the value is
present in the tree, it will only be in the left sub-tree, as 29 is smaller
than 39 (the root node’s value). The left sub-tree has a root node with
the value 27. Since 29 is greater than 27, we will move to the right
sub-tree, where we will find the element.
• Thus, the average running time of a search operation is O(log2 n)
continue
• Binary search trees are considered to
be efficient data structures especially
when compared with sorted linear
arrays and linked lists. In a sorted array,
searching can be done in O(log2 n)
time, but insertions and deletions are
quite expensive.
• In contrast, inserting and deleting
elements in a linked list is easier, but
searching for an element is done in
O(n) time.
• However, in the worst case, a binary
search tree will take O(n) time to
search for an element. The worst case
would occur when the tree is a linear
chain of nodes as given in Fig.
To summarize
• A binary search tree is a binary tree with the following properties:
• The left sub-tree of a node N contains values that are less than N’s
value.
• The right sub-tree of a node N contains values that are greater than
N’s value.
• Both the left and the right binary trees also satisfy these properties
and, thus, are binary search trees.
TRAVERSING A BINARY TREE
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
• Unlike linear data structures in which the elements are traversed
sequentially, tree is a non linear data structure in which the elements
can be traversed in many different ways.
• There are different algorithms for tree traversals. These algorithms
differ in the order in which the nodes are visited.
Pre-order Traversal
In-order Traversal
Post-order Traversal
Pre-order Traversal
• To traverse a non-empty binary tree in pre-order, the
following operations are performed recursively at each
node. The algorithm works by:
• 1. Visiting the root node,
• 2. Traversing the left sub-tree, and finally
• 3. Traversing the right sub-tree.
Consider the tree given in Fig. The pre-order traversal of the
tree is given as A, B, C. Root node first, the left sub-tree next,
and then the right sub-tree. Pre-order traversal is also called
as depth-first traversal. In this algorithm, the left sub-tree is
always traversed before the right sub-tree. The word ‘pre’ in
the pre-order specifies that the root node is accessed prior
to any other nodes in the left and right sub-trees. Pre-order
algorithm is also known as the NLR traversal algorithm
(Node-Left-Right).
Algorithm for preoreder travarsal
• Step 1: Repeat Steps 2 to 4 while TREE != NULL
• Step 2: Write TREE-> DATA
• Step 3: PREORDER(TREE-> LEFT)
• Step 4: PREORDER(TREE ->RIGHT)
[END OF LOOP]
• Step 5: END
Preorder example

TRAVERSAL ORDER for (a):


A, B, D, G, H, L, E, C, F, I, J, and K

TRAVERSAL ORDER for(b):


A, B, D, C, E, F, G, H, and I
In-order Traversal
• To traverse a non-empty binary tree in in-order, the following
operations are performed recursively at each node.
• The algorithm works by:
1. Traversing the left sub-tree,
2. Visiting the root node, and finally
3. Traversing the right sub-tree.
The in-order traversal of the tree is given as B, A, and C. Left
sub-tree first, the root node next, and then the right sub-tree.
In-order traversal is also called as symmetric traversal. In this
algorithm, the left sub-tree is always traversed before the root
node and the right sub-tree. The word ‘in’ in the in-order
specifies that the root node is accessed in between the left and
the right sub-trees. In-order algorithm is also known as the LNR
traversal algorithm (Left-Node-Right).
Algorithm for in-order travarsal
• Step 1: Repeat Steps 2 to 4 while TREE != NULL
• Step 2: INORDER(TREE->LEFT)
• Step 3: Write TREE->DATA
• Step 4: INORDER(TREE-> RIGHT)
[END OF LOOP]
• Step 5: END
Inorder example

• TRAVERSAL ORDER for(a):


G, D, H, L, B, E, A, C, I, F, K, and J

• TRAVERSAL ORDER for (b):


B, D, A, E, H, G, I, F, and C
Post-order Traversal
• To traverse a non-empty binary tree in post-order,
the following operations are performed recursively at
each node. The algorithm works by:
1. Traversing the left sub-tree,
2. Traversing the right sub-tree, and finally
3. Visiting the root node.
The post-order traversal of the tree is given as B, C,
and A. Left sub-tree first, the right sub-tree next,
• and finally the root node. In this algorithm, the
• left sub-tree is always traversed before the right
• sub-tree and the root node. The word ‘post’ in
• the post-order specifies that the root node is
• accessed after the left and the right sub-trees.
• Post-order algorithm is also known as the LRN
• traversal algorithm (Left-Right-Node).
Algorithm of post order travarsal
• Step 1: Repeat Steps 2 to 4 while TREE != NULL
• Step 2: POSTORDER(TREE-> LEFT)
• Step 3: POSTORDER(TREE-> RIGHT)
• Step 4: Write TREE-> DATA
• [END OF LOOP]
• Step 5: END
postorder example
• TRAVERSAL ORDER (for a):
G, L, H, D, E, B, I, K, J, F, C, and A

• TRAVERSAL ORDER for b:


D, B, H, I, G, F, E, C, and A
Expression tree
• Binary trees are widely used to store
algebraic expressions.
• For example, consider the algebraic
expression given as:
Exp = (a – b) + (c * d)
• This expression can be represented
using a binary tree as shown in Fig.
9.13.
Heap tree
• A Heap is a complete binary tree data
structure that satisfies the heap
property:
• A complete binary tree is a binary tree
in which all the levels except the last
level, i.e., leaf node should be
completely filled, and all the nodes
should be left-justified.
• Heaps are usually used to implement
priority queues, where the smallest (or
• In the above figure, we can observe that all the
internal nodes are completely filled except the largest) element is always at the root of
leaf node; therefore, we can say that the above
tree is a complete binary tree.
the tree
There are two types of the heap:

• Min Heap: The value of the • Max Heap: The value of the
parent node should be less parent node is greater than or
than or equal to either of equal to its children.
its children.

You might also like