Meeting 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Trees

MEETING 3
Introduction
• A tree is a data structure that stores elements hierarchically.
• Each element in a tree has a parent element and zero or more children
elements, Except the Top Element.

• the top element is called the root of the tree.


• A tree 𝑇 is a set of nodes storing elements in a parent-child
relationship with the following properties:
• 𝑇 has a special node 𝑟, called the root of 𝑇, with no parent node.
• Each node 𝑣 of 𝑇 different from 𝑟 has a unique parent node 𝑢.
• A tree cannot be empty, since it must have at least one node, the
root.
• If node 𝑢 is the parent of node 𝑣, then we say that 𝑣 is a child of 𝑢.
• Two nodes that are children of the same parent are siblings.
• A node is external if it has no children, External nodes are also
known as leaves.
• A node is internal if it has one or more children.
• The subtree of 𝑇 rooted at a node 𝑣 is the tree consisting of
all the descendants of 𝑣 in 𝑇 (including 𝑣 itself).
• An ancestor of a node is either the node itself, its parent, or
an ancestor of its parent.
• A node 𝑣 is a descendant of a node 𝑢 if 𝑢 is an ancestor of
𝑣.
Binary Tree
• A binary tree is an ordered tree in which every node has at
most two children.
• A binary tree is proper if each internal node has two
children.
• For each internal node in a binary tree, we label each child
as either being a left child or a right child.
• Children are ordered so that a left child comes before a
right child.
Binary Tree – Arithmetic Expression Application
Tree – Functions or Methods
• root(): Return the root of the tree.
• parent(𝑣): Return the parent of node 𝑣; an error occurs if 𝑣 is root.
• children(𝑣): Return a set containing the children of node 𝑣.
• isInternal(𝑣): Test whether node 𝑣 is internal.
• isExternal(𝑣): Test whether node 𝑣 is external.
• isRoot(𝑣): Test whether node 𝑣 is the root.
Tree – Functions or Methods
• size(): Return the number of nodes in the tree.
• elements(): Return a set containing all the elements stored at nodes of the tree.
• positions(): Return a set containing all the nodes of the tree.
• swapElements(𝑣,𝑤): Swap the elements stored at the nodes 𝑣 and 𝑤.
• replaceElement(𝑣, e): Replace with e and return the element stored at node 𝑣.
Tree Traversal
• Depth and Height
Tree Traversal
• Height of a tree
• The height of a tree T is equal to
the maximum depth of an external
node of T.
Tree Traversal
• There are three commonly used patterns to visit all the nodes in a tree.
The difference between these patterns is the order in which each node is
visited. We call this visitation of the nodes a “traversal.” The three
traversals we will look at are called preorder, inorder, and postorder.
• Preorder: in a preorder traversal, we visit the root node first, then
recursively do a preorder traversal of the left subtree, followed by a
recursive preorder traversal of the right subtree.
• Inorder: in an inorder traversal, we recursively do an inorder traversal on
the left subtree, visit the root node, and finally do a recursive inorder
traversal of the right subtree.
• Postorder: in a postorder traversal, we recursively do a postorder traversal
of the left subtree and the right subtree followed by a visit to the root
node.
Tree Traversal – Preorder Traversal
• In this traversal method, the root node is visited first,
then the left sub-tree and finally the right sub-tree.
• We start from A, and following pre-order traversal,
we first visit A itself and then move to its left sub-
tree B. B is also traversed pre-order. 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
Tree Traversal – Preorder Traversal
Preorder Traversal of a Binary Tree
Tree Traversal – Postorder Traversal
• In this traversal method, the root node is visited
last, hence the name. First we traverse the left sub-
tree, then the right sub-tree and finally the root
node.
• We start from A, and following pre-order traversal,
we first visit the left sub-tree B. B is also traversed
post-order. 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
Tree Traversal – Postorder Traversal
Postorder Traversal of a Binary Tree
Inorder Traversal of a Binary Tree
• In this traversal method, the left sub-tree is visited
first, then the root and later the right sub-tree. We
should always remember that every node may
represent a sub-tree itself. If a binary tree is
traversed in-order, the output will produce sorted
key values in an ascending order.
• We start from A, and following in-order traversal, we
move to its left sub-tree B. B is also traversed in-
order. The process goes on until all the nodes are
visited. The output of inorder traversal of this tree
will be:
• D→B→E→A→F→C→G
Inorder Traversal of a Binary Tree
Example
The Binary Search Tree (BST) Algorithm
Recursion
What Is Recursion?
• Recursion is a method of solving problems that involves breaking a
problem down into smaller and smaller subproblems until you get
to a small enough problem that it can be solved trivially.
• Usually recursion involves a function calling itself. While it may not
seem like much on the surface, recursion allows us to write elegant
solutions to problems that may otherwise be very difficult to
program.
Analysis of recursive algorithm: back substitution method
Example
• T(n) = c + T (n-1) (1)
• Which means that the time complexity of the time series T(n) is equal to constant
(denoted by c) that represents a constant amount of operations that should occur (for
example checking if n is greater than 1 ) in addition to the call of T(n-1)
• Using back substitution method:
A(n)
• T(n-1) = c + T(n-2) (2)
{
• T(n-2) = c + T(n-3) (3)
if (n > 1)
• From equations 1, 2, and 3
return A(n-1)
• At step K  T(n) = kc + T(n-k)
else
• This T(n) will stop once n-k = 1 which implies that k = n-1
return 1
• And hence  T(n) = (n-1) c + T(n-(n-1)) = n c – c +1  O(n)
Analysis of recursive algorithm: back substitution method
Example
= n + T (n-1) at n>1 (1)
T(n)
=1 elsewhere

Solution: Using back substitution method:


T(n-1) = n-1 + T(n-2) (2)
T(n-2) = n-2 + T(n-3) (3)
Sum of Arithmetic Series
From equations 1, 2, and 3
At step K  T(n) = n + (n-1) + (n-2)+ … + (n-k) + T(n-(k+1))
This T(n) will stop once n-k -1= 1 which implies that k = n-2
And hence  T(n) = n + (n-1) + (n-2)+ … + (n-k) + 1
T(n) = n + (n-1) + (n-2)+ … + 2+ 1
= n(n+1)/2  𝑂 𝑛2
Analysis of recursive algorithm: back substitution method
Example
= 2 T (n / 2) + c at n>1 (1)
T(n)
=1 elsewhere

Solution:

Sum of Geometric Series

You might also like