0% found this document useful (0 votes)
2 views37 pages

Lesson6 Trees

This document covers the fundamentals of trees, a hierarchical data structure, including definitions, types such as binary trees and binary search trees, and their properties. It explains tree operations like search and insert, as well as traversal methods (in-order, pre-order, post-order) and their algorithms. Additionally, it discusses the advantages and disadvantages of binary search trees and their applications in various fields.
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)
2 views37 pages

Lesson6 Trees

This document covers the fundamentals of trees, a hierarchical data structure, including definitions, types such as binary trees and binary search trees, and their properties. It explains tree operations like search and insert, as well as traversal methods (in-order, pre-order, post-order) and their algorithms. Additionally, it discusses the advantages and disadvantages of binary search trees and their applications in various fields.
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/ 37

DIT2109:

DATA STRUCTURES AND ALGORITHMS


TREES
LEARNING OBJECTIVES
 After completing this chapter, you will be able to
1. Describe a tree
2. Explain how a tree can be represented internally
3. Describe how a tree can be traversed.
TREE
 Tree is a collection of nodes represented in a hierarchical
fashion, with a specially designated node called root.
 Except root all other nodes have parent in their higher
hierarchy.
 Tree represents nodes connected by edges.
 Tree is one of the several types of non-linear data structure.
 A parent node of a particular node is the one which is in the
higher hierarchy for a A node can have exactly one parent i.e. a
node can be attached to exactly one node in its higher
hierarchy.
EXAMPLE OF A TREE
TERMS
EXAMPLE
THE FOLLOWING TABLE DEPICTS SOME OF THE
IMPORTANT TERMINOLOGIES RELATED TO A GENERAL
TREE STRUCTURE.
BINARY TREE (BINARY SEARCH TREE)-BST
 Binary Tree is an ordered tree whose vertices have at
most two children. The children are distinguished as
the left and Right child.
 Binary Tree consist of a root and two disjoint binary
trees, called the left and right sub-trees. In other words
it can be defined as a tree in which all the nodes can
have 2 as a maximum degree i.e. a node can have
maximum two children.
CONT.…

 A binary tree differs from a general tree in the


following aspects:
1. A tree must have at least one node but a binary
tree may be empty.
2. A tree may have any number of sub-trees but a
binary tree can have at most two.
CONT.…
 Binary search tree BST is a tree in which all nodes follows the below
mentioned properties −
1. The left sub-tree of a node has key less than or equal to its
parent node's key.
2. The right sub-tree of a node has key greater than or equal to
its parent node's key.
Thus, a binary search tree BST divides all its sub-trees into
two segments; left sub-tree and right sub-tree and can be
defined as −
CONT.….
 A node's left child must have value less than its parent's value
and node's right child must have value greater than it's
parent value.

BST is a collection of nodes arranged in a way where they maintain BST


properties. Each node has key and associated value. While searching, the
desired key is compared to the keys in BST and if found, the associated value is
retrieved.
CONT.….

 We observe that the root node key 27 has all less-


valued keys on the left sub-tree and higher valued
keys on the right sub-tree.
TYPES OF BINARY TREES

1. Full Binary Tree


2. Complete Binary Tree
3. Skewed binary tree
FULL BINARY TREE
 Full Binary tree:
 A binary tree in which all its leaf nodes are in the same level is
called a full binary tree.
COMPLETE BINARY TREE
 A binary tree in which the array representation is contiguous
without any null pointers in between is a complete binary tree.
SKEWED BINARY TREE
 A binary tree is a skewed binary tree, if it has only left child
(skewed left) or only right (skewed right) child for all its internal
nodes.
HEAPS
 A vertex in a binary tree has the Heap-order property if the value
stored at the vertex is greater than or equal to the values stored at its
descendants.
 Heap: a complete binary tree whose every vertex has the heap-order
property.
 An arbitrary complete binary tree can be made into a heap as follows:
 Every leaf already has the heap-order property, so the sub-trees whose roots
are leaves are heaps.
 Starting with the right-most internal vertex V at the next-to-last level and
working left across levels and upwards in the tree, do the following: if vertex
V does not have the order property, swap its value with the largest of its
children, then do the same with the modified vertex, until the sub-tree
rooted at V is a heap.
BASIC OPERATIONS

 Following are basic primary operations of a tree which are


following
SEARCH OPERATION

 Whenever an element is to be search.


 Start search from root node then if data is less than key
value, search element in left sub-tree otherwise search
element in right sub-tree.
 Follow the same algorithm for each node.
CONT. …
INSERT OPERATION
 Whenever an element is to be inserted.
 First locate its proper location.
 Start search from root node then if data is less than key
value, search empty location in left sub-tree and insert the
data.
 Otherwise search empty location in right sub-tree and
insert the data.
TREE TRAVERSAL
 Traversal is a process to visit all the nodes of a tree and may
print their values too.
 Because, all nodes are connected via edges links we always
start from the root head node. That is, we cannot random
access a node in tree.
 Generally we traverse a tree to search or locate given item or
key in the tree or to print all the values it contains.
 There are three ways which we use to traverse a tree −
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal
INORDER TRAVERSAL
 In this traversal method, the left left-subtree is
visited first, then root and then the right sub-
tree.
 We should always remember that every node may
represent a subtree itself.
 If a binary tree is traversed inorder, the output will
produce sorted key values in ascending order
INORDER TRAVERSAL

Access the tree from A, and following in-order traversal, we move to its left
subtree B.
B is also traversed in-ordered. And the process goes on until all the nodes are
visited.The output of in-order traversal of this tree will be −
D → B → E →A → F → C → G
ALGORITHM: INORDER TRAVERSAL
PREORDER TRAVERSAL
 In this traversal method, the root node is visited first, then
left subtree and finally right sub-tree.
CONT.…

 We start from A, and following pre-order traversal, we first


visit A itself and then move to its left subtree B.
 B is also traversed pre-ordered. And the process goes on
until all the nodes are visited.
 The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
ALGORITHM: PREORDER TRAVERSAL
POSTORDER TRAVERSAL

 In this traversal method, the root node is visited last,


hence the name.
 First we traverse left subtree, then right subtree
and finally root.
CONT.…

We start from A, and following pre-order traversal, we first visit left subtree B.
B is also traversed post-ordered. And the process goes on until all the nodes are
visited.The output of post-order traversal of this tree will be −
D → E → B → F → G → C →A
ALGORITHM: POSTORDER TRAVERSAL

 Until all nodes are traversed −


 Step 1 − Recursively traverse left subtree.
 Step 2 − Recursively traverse right subtree.
 Step3 − Visit root node.
SIMPLE STEPS IN TRAVERSALS
ADVANTAGE OF A BST

 Searching a node in a BST is faster, since either left or


right sub tree is only searched from the root till the
node is found instead of comparing all the nodes
preceding it.
DISADVANTAGE OF A BST

 The tree may be a skewed binary tree if the


elements are either in ascending(skewed left) or in
descending(skewed right) order, which lead to more
levels.
SOME APPLICATIONS OF TREES ARE:

1. Representing family genealogy


2. As the underlying structure in decision-making algorithms
3. To represent priority queues (a special kind of tree called a
heap)
4. To provide fast access to information in a database (a special
kind of tree called a btree)

You might also like