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

trees

Uploaded by

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

trees

Uploaded by

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

21CSC201J

DATA STRUCTURES AND ALGORITHMS

UNIT-4
Topic : Trees
TREES
Introduction to Trees
• The study of trees in mathematics can be traced to Gustav Kirchhoff in the
middle nineteenth century and several years later to Arthur Cayley, who
used trees to study the structure of algebraic formulas.
• Cayley’s work undoubtedly laid the framework for Grace Hopper’s use of
trees in 1951 to represent arithmetic expressions.
• Hopper’s work bears a strong resemblance to today’s binary tree formats.
• Trees are used extensively in computer science to represent algebraic
formulas;
• as an efficient method for searching large, dynamic lists; and for such
diverse applications as artificial intelligence systems and encoding
algorithms.
Basic Tree Concepts

• A tree consists of a finite set of elements, called nodes, and a finite


set of directed lines, called branches, that connect the nodes.
• The number of branches associated with a node is the degree of the
node.
• When the branch is directed toward the node, it is an indegree
branch; when the branch is directed away from the node, it is an
outdegree branch.
• The sum of the indegree and outdegree branches is the degree of
the node.
• A tree consists of a finite set of elements, called nodes, and a finite
set of directed lines, called branches, that connect the nodes.
TREE
TREE Terminology
• If the tree is not empty, the first node is called the root.
• The indegree of the root is, by definition, zero.
• With the exception of the root, all of the nodes in a tree must have an
indegree of exactly one; that is, they may have only one predecessor.
• All nodes in the tree can have zero, one, or more branches leaving
them; that is, they may have an outdegree of zero, one, or more (zero
or more successors). Figure 6-1 is a representation of a tree.
TREE Terminology

• A leaf is any node with an outdegree of zero, that is, a node with no
successors.
• A node that is not a root or a leaf is known as an internal node
because it is found in the middle portion of a tree.
• A node is a parent if it has successor nodes—that is, if it has an
outdegree greater than zero.
• Conversely, a node with a predecessor is a child.
• A child node has an indegree of one.
• Two or more nodes with the same parent are siblings
• An ancestor is any node in the path from the root to the node.
• A descendent is any node in the path below the parent node; that is,
all nodes in the paths from a given node to a leaf are descendents of
that node
PATH
• Several terms drawn from mathematics or
created by computer scientists are used to
describe attributes of trees and their nodes
• A path is a sequence of nodes in which
each node is adjacent to the next one.
• Every node in the tree can be reached by
following a unique path starting from the
root.
• In Figure 6-2 the path from the root to the
leaf I is designated as AFI.
• It includes two distinct branches, AF and FI.
LEVEL

• The level of a node is its distance from the root.


Because the root has a zero distance from itself, the
root is at level 0.
• The children of the root are at level 1, their children are
at level 2, and so forth.
• Note the relationship between levels and siblings in
Figure 6-2.
• Siblings are always at the same level, but all nodes in a
level are not necessarily siblings.
• For example, at level 2, C and D are siblings, as are G, H,
and I.
• However, D and G are not siblings because they have
different parents
HEIGHT
• The height of the tree is the level of the leaf
in the longest path from the root plus 1.
• By definition the height of an empty tree is
-1.
• Figure 6-2 contains nodes at three levels: 0,
1, and 2. Its height is 3.
• Because the tree is drawn upside down,
some texts refer to the depth of a tree
rather than its height.
• The level of a node is its distance from the
root. The height of a tree is the level of the
leaf in the longest path from the root plus
1.
SUBTREES

• A tree may be divided into subtrees.


• A subtree is any connected structure below the
root.
• The first node in a subtree is known as the root of
the subtree and is used to name the subtree.
• Subtrees can also be further subdivided into
subtrees.
• In Figure 6-3, BCD is a subtree, as are E and FGHI.
Note that by this definition, a single node is a
subtree.
• Thus, the subtree B can be divided into two
subtrees, C and D, and the subtree F contains the
subtrees G, H, and I.
TREE DEFN
• A tree is a set of nodes that • The concept of subtrees leads us
either: to a recursive definition of a
• 1. Is empty, or tree: A tree
• 2. Has a designated node, called • is a set of nodes that either: (1)
the root, from whi is empty or (2) has a designated
node, called the root, from
which hierarchically descend
zero or more subtrees, which are
also trees.
User Representation

• There are three different user


representations for trees. The first is
the organization chart format, which
is basically the notation we use to
represent
• trees in our figures. The term we use
for this notation is general tree. The
general
• tree representation of a computer’s
components is shown in Figure
Binary Trees
• A binary tree is a tree in which no
node can have more than two
subtrees;
• the maximum outdegree for a node is
two.
• In other words, a node can have zero,
one, or two subtrees.
• These subtrees are designated as the
left subtree and the right subtree.
• Figure 6-5 shows a binary tree with its
two subtrees. Note that each subtree
is itself a binary tree.
Binary Tree Properties
• Height of Binary Trees
• Maximum Height
• Given that we need to store N nodes in a binary tree, the maximum height,
• Hmax, is
Example
• Given three nodes to be stored in a binary tree, what is the maximum
height?
• In this example N is 3. Therefore, the maximum height is 3. There are
four different trees that can be drawn to satisfy a maximum height of
3. A tree with a maximum height is rare.
Minimum Height

• The minimum height of the tree, Hmin, is determined by the following


formula:
• Hmin = log2N + 1
• Example Given three nodes to be stored in a binary tree, what is the minimum
height?
• Again, N is 3. Therefore, the minimum height is 2.
• Minimum Nodes.
• We can turn the calculation around and determine the minimum number of
nodes in a tree of a specified height. Given a height of the binary tree, H, the
minimum number of nodes in the tree are given as
• Nmin = H
Example
• Given a tree of height 3, what is the minimum number of nodes that
can be
• stored? Using the formula for Nmin, the answer is H, or 3 nodes. This
case is
• seen in Figure 6-6(g) and 6-6(h).
Maximum Nodes
• The formula for the maximum number of nodes is derived from the
fact that each node can have only two descendents. Given a height of
the binary tree, H, the maximum number of nodes in the tree is given
as

Given a tree of height 3, what is the maximum number of


nodes that can be stored? Nmax is 7.
Balance
• To determine whether a tree is balanced, we calculate its balance
• factor. The balance factor of a binary tree is the difference in height
between its
• left and right subtrees. If we define the height of the left subtree as
HL and the
• height of the right subtree as HR, the balance factor of the tree, B, is
determined
• by the following formula:
Using this formula, the
balances of the eight trees
in Figure 6-6 are (a) 0
by definition, (b) 0, (c) 1,
(d) –1, (e) 0, (f) 1, (g) –2,
and (h) 2.
Complete and Nearly Complete Binary Trees

• A complete tree has the maximum number of entries for its height (see
formula
Nmax in “Height of Binary Trees”). The maximum number is reached when
the
last level is full (see Figure 6-7). A tree is considered nearly complete if it
has the
minimum height for its nodes (see formula Hmin) and all nodes in the last
level are found on the left. Complete and nearly complete trees are shown
in
Figure 6-7.
Binary Tree Traversals
• A binary tree traversal requires that each node of the tree be
processed once and only once in a predetermined sequence.
• The two general approaches to the traversal sequence are depth first
and breadth first.
• In a breadth-first traversal, the processing proceeds horizontally from
the root to all of its children, then to its children’s children, and so
forth until all nodes have been processed. In other words, in the
breadth-first traversal, each level is completely processed before the
next level is started.
Depth-first Traversals
• Given that a binary tree consists of a root, a left subtree, and a right
subtree,we can define six different depth-first traversal sequences.
Binary Search Trees-Basic Concepts

A binary search tree (BST) is a binary tree with the


following properties:
• All items in the left subtree are less than the root.
• All items in the right subtree are greater than or equal to
the root.
• Each subtree is itself a binary search tree.
Searches-Find the Smallest Node
Algorithm findSmallestBST (root)
This algorithm finds the smallest node in a BST.
Pre root is a pointer to a nonempty BST or subtree
Return address of smallest node
1 if (left subtree empty)
1 return (root)
2 end if
3 return findSmallestBST (left subtree)
end findSmallestBST
Find the Largest Node
This time we start at the tree root and follow the right branches to the far-right node in the
tree, which by definition must be the largest. The pseudocode is shown in Algorithm 7-2.
Algorithm findLargestBST (root)
This algorithm finds the largest node in a BST.
Pre root is a pointer to a nonempty BST or subtree
Return address of largest node returned
1 if (right subtree empty)
1 return (root)
2 end if
3 return findLargestBST (right subtree)
end findLargestBST
Search BST
Algorithm searchBST (root, targetKey) 3 if (targetKey < root)
Search a binary search tree for a given value. 1 return searchBST (left subtree,
Pre root is the root to a binary tree or targetKey)
subtree
4 else if (targetKey > root)
targetKey is the key value requested
1 return searchBST (right subtree,
Return the node address if the value is found
targetKey)
null if the node is not in the tree
5 else
1 if (empty tree)
Not found
Found target key
1 return null 1 return root
2 end if 6 end if
end searchBST
Insertion
Binary Search Tree ADT

You might also like