SlideShare a Scribd company logo
First Year BA (IT)
CT1120 Algorithms
Lecture 19
Dr. Zia Ush Shamszaman
z.shamszaman1@nuigalway.ie
1
School of Computer Science, College of Science and Engineering
28-02-2020
Overview
•  Binary Search Tree (BST)
–  BST Algorithms
–  Python code for BST
–  AVL
–  Red-Black Tree
–  Splay Tree
–  Problem Solving using BST in Python
•  Feedback and Assessment
228-02-2020
Binary Search
Tree (BST)
•  Binary Search Tree is a node-based binary
tree data structure which has the following
properties
–  The left subtree of a node contains only nodes
with keys lesser than the node’s key.
–  The right subtree of a node contains only nodes
with keys greater than the node’s key.
–  The left and right subtree each must also be a
binary search tree.
28-02-2020 3
BST Example
28-02-2020 4
How to search?
28-02-2020 5
Insertion in a BST
28-02-2020 6
Deletion (Leaf)
28-02-2020 7
Deletion (Internal Node)
28-02-2020 8
Deletion (Internal Node)
28-02-2020 9
BST Animation
28-02-2020 10
https://www.cs.usfca.edu/~galles/visualization/BST.html
BT vs BST
•  A binary tree is simply a tree in which each node can have at
most two children.
•  A binary search tree is a binary tree in which the nodes are
assigned values, with the following restrictions :
–  No duplicate values.
–  The left subtree of a node can only have values less than the node
–  The right subtree of a node can only have values greater than the
node and recursively defined
–  The left subtree of a node is a binary search tree.
–  The right subtree of a node is a binary search tree.
28-02-2020 11
BST Traversal
28-02-2020 12
Inorder
Preorder
Postorder
BST Traversal
28-02-2020 13
Inorder
Preorder
Postorder
Algorithm of BST
28-02-2020 14
A) compare ITEM with the root node N of the tree
i) if ITEM < N, proceed to the left child of N.
ii) if ITEM > N, proceed to the right child of N.
B) repeat step (A) until one of the following occurs
i) we meet a node N such that ITEM=N, i.e.
search is successful.
ii) we meet an empty sub tree, i.e. the search is
unsuccessful.
Types of BST
28-02-2020 15
Self Balancing
Adelson-Velskii and Landis (AVL) Tree
28-02-2020 16
•  An AVL tree is a binary search tree which has the
following properties:
•  The sub-trees of every node differ in height by
at most one.
•  Every sub-tree is an AVL tree.
Balance requirement for an AVL
tree: the left and right sub-trees
differ by at most 1 in height.
AVL Tree?
28-02-2020 17
AVL Tree?
28-02-2020 18
YES
Examination shows that each left sub-tree has a height
1 greater than each right sub-tree.
NO
Sub-tree with root 8 has height 4 and sub-
tree with root 18 has height 2
Red-Black Tree
28-02-2020 19
•  Red-Black Tree is a self-balancing Binary Search
Tree (BST) where every node follows following rules:
•  Every node has a color either red or black.
•  Root of tree is always black.
•  There are no two adjacent red nodes (A red node
cannot have a red parent or red child).
•  Every path from a node (including root) to any of
its descendant NIL node has the same number of
black nodes.
Splay Tree
28-02-2020 20
•  Splay tree provides performance better by
optimizing the placement of nodes.
•  It makes the recently accessed nodes in the top of
the tree and hence providing better performance.
•  It’s suitable for cases where there are large
number of nodes but only few of them are accessed
frequently.
https://www.cs.usfca.edu/~galles/visualization/SplayTree.html
Browse the link below for visulalization of Splay tree
28-02-2020 21
# A O(n^2) Python3 program for construction of BST from preorder
traversal
# A binary tree node
class Node():
# A constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# constructTreeUtil.preIndex is a static variable of
# function constructTreeUtil
# Function to get the value of static variable
# constructTreeUtil.preIndex
def getPreIndex():
return constructTreeUtil.preIndex
# Function to increment the value of static variable
# constructTreeUtil.preIndex
def incrementPreIndex():
constructTreeUtil.preIndex += 1
# A recurseive function to construct Full from pre[].
# preIndex is used to keep track of index in pre[[].
def constructTreeUtil(pre, low, high, size):
# Base Case
if( getPreIndex() >= size or low > high):
return None
# The first node in preorder traversal is root. So take
# the node at preIndex from pre[] and make it root,
# and increment preIndex
root = Node(pre[getPreIndex()])
incrementPreIndex()
# If the current subarray has onlye one element,
# no need to recur
if low == high :
return root
# Search for the first element greater than root
for i in range(low, high+1):
if (pre[i] > root.data):
break
# Use the index of element found in preorder to divide
# preorder array in two parts. Left subtree and right
# subtree
root.left = constructTreeUtil(pre, getPreIndex(), i-1 , size)
root.right = constructTreeUtil(pre, i, high, size)
return root
# The main function to construct BST from given preorder
# traversal. This function mailny uses constructTreeUtil()
def constructTree(pre):
size = len(pre)
constructTreeUtil.preIndex = 0
return constructTreeUtil(pre, 0, size-1, size)
def printInorder(root):
if root is None:
return
printInorder(root.left)
print root.data,
printInorder(root.right)
# Driver program to test above function
pre = [10, 5, 1, 7, 40, 50]
root = constructTree(pre)
print "Inorder traversal of the constructed tree:"
printInorder(root)
Python code for Preorder BST
Problem-1
•  Write a Python program to create a Balanced
Binary Search Tree (BST) using an array (given)
elements where array elements are sorted in
ascending order.
28-02-2020 22
Online Python Compilter:
http://pythontutor.com/live.html#mode=edit
https://www.onlinegdb.com/online_python_debugger
28-02-2020 23
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def sorted_array_to_bst(nums):
if not nums:
return None
mid_val = len(nums)//2
node = TreeNode(nums[mid_val])
node.left =
sorted_array_to_bst(nums[:mid_val
])
node.right =
sorted_array_to_bst(nums[mid_val+
1:])
return node
def preOrder(node):
if not node:
return
print(node.val)
preOrder(node.left)
preOrder(node.right)
result = sorted_array_to_bst([1, 2,
3, 4, 5, 6, 7])
preOrder(result)
Solution of the Problem 1
Problem 2
•  Write a Python program to find the closest
value of a given target value in a given non-
empty Binary Search Tree (BST) of unique
values.
28-02-2020 24
Online Python Compilter:
http://pythontutor.com/live.html#mode=edit
https://www.onlinegdb.com/online_python_debugger
Solution of the Problem 2
28-02-2020 25
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def closest_value(root, target):
a = root.val
kid = root.left if target < a else
root.right
if not kid:
return a
b = closest_value(kid, target)
return min((a,b), key=lambda x:
abs(target-x))
root = TreeNode(8)
root.left = TreeNode(5)
root.right = TreeNode(14)
root.left.left = TreeNode(4)
root.left.right = TreeNode(6)
root.left.right.left = TreeNode(8)
root.left.right.right = TreeNode(7)
root.right.right = TreeNode(24)
root.right.right.left = TreeNode(22)
result = closest_value(root, 19)
print(result)
Problem 3
•  Write a Python program to check whether a given a binary tree is a valid
binary search tree (BST) or not
28-02-2020 26
Let a binary search tree (BST) is defined as follows:
The left subtree of a node contains only nodes with keys less than the
node's key.
The right subtree of a node contains only nodes with keys greater than the
node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:
2
/ 
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ 
2 3
Binary tree [1,2,3], return false.
Problem 4
28-02-2020 27
•  Write a Python program to delete a node with
the given key in a given Binary search tree
(BST).
Note: Search for a node to remove. If the node is found,
delete the node.
Solution to the Problem 4
28-02-2020 28
# Definition: Binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def delete_Node(root, key):
# if root doesn't exist, just return it
if not root:
return root
# Find the node in the left subtree if key value is less than root value
if root.val > key:
root.left = delete_Node(root.left, key)
# Find the node in right subtree if key value is greater than root value,
elif root.val < key:
root.right= delete_Node(root.right, key)
# Delete the node if root.value == key
else:
# If there is no right children delete the node and new
root would be root.left
if not root.right:
return root.left
# If there is no left children delete the node and new
root would be root.right
if not root.left:
return root.right
# If both left and right children exist in the node replace its value with
# the minmimum value in the right subtree. Now delete that minimum
node
# in the right subtree
temp_val = root.right
mini_val = temp_val.val
while temp_val.left:
temp_val = temp_val.left
mini_val = temp_val.val
# Replace value
root.val = mini
# Delete the minimum node in right subtree
root.right = deleteNode(root.right,root.val)
return root
def preOrder(node):
if not node:
return
print(node.val)
preOrder(node.left)
preOrder(node.right)
root = TreeNode(5)
root.left = TreeNode(3)
root.right = TreeNode(6)
root.left.left = TreeNode(2)
root.left.right = TreeNode(4)
root.left.right.left = TreeNode(7)
print("Original node:")
print(preOrder(root))
result = delete_Node(root, 4)
print("After deleting specified node:")
print(preOrder(result))
Next Lecture
28-02-2020 29
Useful Links	
•  http://www.csanimated.com/animation.php?t=Quicksort
•  http://www.hakansozer.com/category/c/
•  http://www.nczonline.net/blog/2012/11/27/computer-science-in-
javascript-quicksort/
•  http://www.sorting-algorithms.com/shell-sort
•  https://www.tutorialspoint.com/
•  https://www.hackerearth.com/
•  www.khanacademy.org/computing/computer-science/algorithms
•  https://www.w3resource.com/python-exercises/data-structures-
and-algorithms/python-binary-search-tree-index.php#EDITOR
•  https://medium.com/@stephenagrice/how-to-implement-a-binary-
search-tree-in-python-e1cdba29c533
28-02-2020 30
Feedback & Assessment
28-02-2020 31
28-02-2020 32
L-17 Slides
28-02-2020 33
•  Given the following sequence of stack
operations, what is the top item on the stack
when the sequence is complete?
28-02-2020 34
m = Stack()
m.push('x')
m.push('y')
m.pop()
m.push('z')
m.peek()
•  Given the following sequence of stack
operations, what is the top item on the stack
when the sequence is complete?
28-02-2020 35
m = Stack()
m.push('x')
m.push('y')
m.pop()
m.push('z')
m.peek()
What this code is doing?
28-02-2020 36
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
print('nElements poped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('nStack after elements are poped:')
print(stack)
Stack implementation code
28-02-2020 37
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
print('nElements poped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('nStack after elements are poped:')
print(stack)
OUTPUT
Initial stack
['a', 'b', 'c']
Elements poped from stack:
c
b
a
Stack after elements are poped:
[]
What this code is doing?
28-02-2020 38
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
print("nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("nQueue after removing elements")
print(queue)
What this code is doing?
28-02-2020 39
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
print("nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("nQueue after removing elements")
print(queue)
Output
Initial queue
['a', 'b', 'c']
Elements dequeued from queue
a
b
c
Queue after removing elements
[]
Tree
•  A Tree is a collection of elements called nodes.
•  One of the node is distinguished as a root, along with a
relation (“parenthood”) that places a hierarchical
structure on the nodes.
28-02-2020 40
Applications of Tree
28-02-2020 41
Main applications of trees include:
1. Manipulate hierarchical data.
2. Make information easy to search (see tree
traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for
visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see
business chess).
Binary Tree
28-02-2020 42
•  A tree whose elements have at most 2 children is called a
binary tree.
•  Since each element in a binary tree can have only 2
children, we typically name the left child and right child.
Binary Tree
28-02-2020 43
•  A tree whose elements have at most 2 children is called a
binary tree.
•  Since each element in a binary tree can have only 2
children, we typically name the left child and right child.
((7+3)∗(5−2))
Binary Tree
28-02-2020 44
•  A tree whose elements have at most 2 children is called a
binary tree.
•  Since each element in a binary tree can have only 2
children, we typically name the left child and right child.
•  A Binary Tree node contains following parts.
•  Data
•  Pointer to left child
•  Pointer to right child
Python code of
Binary Tree
28-02-2020 45
# A class that represents an individual node in a
# Binary Tree
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
# create root
root = Node(1)
''' following is the tree after above statement
1
/ 
None None'''
root.left = Node(2);
root.right = Node(3);
''' 2 and 3 become left and right children of 1
1
/ 
2 3
/  / 
None None None None''’
root.left.left = Node(4);
'''4 becomes left child of 2
1
/ 
2 3
/  / 
4 None None None
/ 
None None'''
"__init__" is a reseved method in python classes. It is
called as a constructor in object oriented terminology. This
method is called when an object is created from a class
and it allows the class to initialize the attributes of the
class.
Tree traversal
28-02-2020 46
Following are the generally used ways for traversing trees:
•  Inorder: 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.
•  Preorder: 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.
•  Postorder: We recursively do a postorder traversal of
the left subtree and the right subtree followed by a visit
to the root node.
Tree traversal
28-02-2020 47
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Level Order Traversal : 1 2 3 4 5
Tree traversal
28-02-2020 48
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Level Order Traversal : 1 2 3 4 5
Tree traversal code
28-02-2020 49
# A class that represents an individual node in a
Binary Tree
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
# A function to do inorder tree traversal
def printInorder(root):
if root:
# First recur on left child
printInorder(root.left)
# then print the data of node
print(root.val),
# now recur on right child
printInorder(root.right)
# A function to do postorder tree traversal
def printPostorder(root):
if root:
# First recur on left child
printPostorder(root.left)
# the recur on right child
printPostorder(root.right)
# now print the data of node
print(root.val),
# A function to do preorder tree traversal
def printPreorder(root):
if root:
# First print the data of node
print(root.val),
# Then recur on left child
printPreorder(root.left)
# Finally recur on right child
printPreorder(root.right)
# Driver code
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print ("nPreorder traversal of binary tree is")
printPreorder(root)
print ("nInorder traversal of binary tree is")
printInorder(root)
print ("nPostorder traversal of binary tree is")
printPostorder(root)
Inorder traversal without recursion
28-02-2020 50
# A binary tree node
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Iterative function for inorder tree traversal
def inOrder(root):
# Set current to root of binary tree
current = root
stack = [] # initialize stack
done = 0
while True:
# Reach the left most Node of the current Node
if current is not None:
# Place pointer to a tree node on the stack
# before traversing the node's left subtree
stack.append(current)
current = current.left
# BackTrack from the empty subtree and visit the Nod
# at the top of the stack; however, if the stack is
# empty you are done
elif(stack):
current = stack.pop()
print(current.data, end=" ")
# Python 3 printing
# We have visited the node and its left
# subtree. Now, it's right subtree's turn
current = current.right
else:
break
print()
# Driver program to test above function
""" Constructed binary tree is
1
/ 
2 3
/ 
4 5 """
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
inOrder(root)
Priority Queue
28-02-2020 51
•  A priority queue acts like a queue in that you dequeue an item by
removing it from the front.
•  In a priority queue the logical order of items inside a queue is
determined by their priority.
•  The highest priority items are at the front of the queue and the lowest
priority items are at the back.
•  When you enqueue an item on a priority queue, the new item may move
all the way to the front.
Binary Heaps
28-02-2020 52
•  A Heap is a special Tree-based data structure in
which the tree is a complete binary tree. Generally,
Heaps can be of two types:
•  Max-Heap: The key present at the root node must be
greatest among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in
that Binary Tree.
•  Min-Heap: The key present at the root node must be
minimum among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in
that Binary Tree.
Binary Heap Operation
28-02-2020 53
The basic operations we will implement for our
binary heap are as follows:
•  BinaryHeap() creates a new, empty, binary heap.
•  insert(k) adds a new item to the heap.
•  findMin() returns the item with the minimum key value,
leaving item in the heap.
•  delMin() returns the item with the minimum key value,
removing the item from the heap.
•  isEmpty() returns true if the heap is empty, false
otherwise.
•  size() returns the number of items in the heap.
•  buildHeap(list) builds a new heap from a list of keys.

More Related Content

What's hot

Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introduction
Andres Mendez-Vazquez
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Tree
TreeTree
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
HasnainBaloch12
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
Intro C# Book
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
Usha Mahalingam
 
210 trees
210 trees210 trees
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
Eleti Ganapathi
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Rai University
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
Andres Mendez-Vazquez
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queues
Andres Mendez-Vazquez
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
Intro C# Book
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
Hanif Durad
 

What's hot (18)

Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introduction
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Tree
TreeTree
Tree
 
Review session2
Review session2Review session2
Review session2
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
210 trees
210 trees210 trees
210 trees
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queues
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 

Similar to L 19 ct1120

L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
Zia Ush Shamszaman
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
DrBashirMSaad
 
Binary tree
Binary treeBinary tree
Binary tree
Maria Saleem
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
bst-copy-171118141913.pdf
bst-copy-171118141913.pdfbst-copy-171118141913.pdf
bst-copy-171118141913.pdf
ControlUrgentSecurit
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
Topic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.pptTopic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.ppt
plagcheck
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
hardjasonoco14599
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
KeshavBandil2
 
210 trees5
210 trees5210 trees5
210 trees5
Tanweer Page
 
Python Spell Checker
Python Spell CheckerPython Spell Checker
Python Spell Checker
Amr Alarabi
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
VijayaLakshmi506
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
E2
E2E2
E2
lksoo
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
DurgaDeviCbit
 
2021 여름방학 정기 세미나 1주차
2021 여름방학 정기 세미나 1주차2021 여름방학 정기 세미나 1주차
2021 여름방학 정기 세미나 1주차
Moonki Choi
 
Lo27
Lo27Lo27
Lo27
lksoo
 

Similar to L 19 ct1120 (20)

Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
bst-copy-171118141913.pdf
bst-copy-171118141913.pdfbst-copy-171118141913.pdf
bst-copy-171118141913.pdf
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
 
Topic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.pptTopic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.ppt
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
 
210 trees5
210 trees5210 trees5
210 trees5
 
Python Spell Checker
Python Spell CheckerPython Spell Checker
Python Spell Checker
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
E2
E2E2
E2
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
2021 여름방학 정기 세미나 1주차
2021 여름방학 정기 세미나 1주차2021 여름방학 정기 세미나 1주차
2021 여름방학 정기 세미나 1주차
 
Lo27
Lo27Lo27
Lo27
 

More from Zia Ush Shamszaman

Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1
Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
L 18 ct1120
L 18 ct1120L 18 ct1120
L 18 ct1120
Zia Ush Shamszaman
 
L 15 ct1120
L 15 ct1120L 15 ct1120
L 15 ct1120
Zia Ush Shamszaman
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
Zia Ush Shamszaman
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Bangladesh
BangladeshBangladesh
Bangladesh
Zia Ush Shamszaman
 

More from Zia Ush Shamszaman (11)

Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015
 
Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3
 
Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2
 
Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
 
L 18 ct1120
L 18 ct1120L 18 ct1120
L 18 ct1120
 
L 15 ct1120
L 15 ct1120L 15 ct1120
L 15 ct1120
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Bangladesh
BangladeshBangladesh
Bangladesh
 

Recently uploaded

一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
StarCompliance.io
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
James Polillo
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 

Recently uploaded (20)

一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 

L 19 ct1120

  • 1. First Year BA (IT) CT1120 Algorithms Lecture 19 Dr. Zia Ush Shamszaman z.shamszaman1@nuigalway.ie 1 School of Computer Science, College of Science and Engineering 28-02-2020
  • 2. Overview •  Binary Search Tree (BST) –  BST Algorithms –  Python code for BST –  AVL –  Red-Black Tree –  Splay Tree –  Problem Solving using BST in Python •  Feedback and Assessment 228-02-2020
  • 3. Binary Search Tree (BST) •  Binary Search Tree is a node-based binary tree data structure which has the following properties –  The left subtree of a node contains only nodes with keys lesser than the node’s key. –  The right subtree of a node contains only nodes with keys greater than the node’s key. –  The left and right subtree each must also be a binary search tree. 28-02-2020 3
  • 6. Insertion in a BST 28-02-2020 6
  • 11. BT vs BST •  A binary tree is simply a tree in which each node can have at most two children. •  A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions : –  No duplicate values. –  The left subtree of a node can only have values less than the node –  The right subtree of a node can only have values greater than the node and recursively defined –  The left subtree of a node is a binary search tree. –  The right subtree of a node is a binary search tree. 28-02-2020 11
  • 14. Algorithm of BST 28-02-2020 14 A) compare ITEM with the root node N of the tree i) if ITEM < N, proceed to the left child of N. ii) if ITEM > N, proceed to the right child of N. B) repeat step (A) until one of the following occurs i) we meet a node N such that ITEM=N, i.e. search is successful. ii) we meet an empty sub tree, i.e. the search is unsuccessful.
  • 15. Types of BST 28-02-2020 15 Self Balancing
  • 16. Adelson-Velskii and Landis (AVL) Tree 28-02-2020 16 •  An AVL tree is a binary search tree which has the following properties: •  The sub-trees of every node differ in height by at most one. •  Every sub-tree is an AVL tree. Balance requirement for an AVL tree: the left and right sub-trees differ by at most 1 in height.
  • 18. AVL Tree? 28-02-2020 18 YES Examination shows that each left sub-tree has a height 1 greater than each right sub-tree. NO Sub-tree with root 8 has height 4 and sub- tree with root 18 has height 2
  • 19. Red-Black Tree 28-02-2020 19 •  Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: •  Every node has a color either red or black. •  Root of tree is always black. •  There are no two adjacent red nodes (A red node cannot have a red parent or red child). •  Every path from a node (including root) to any of its descendant NIL node has the same number of black nodes.
  • 20. Splay Tree 28-02-2020 20 •  Splay tree provides performance better by optimizing the placement of nodes. •  It makes the recently accessed nodes in the top of the tree and hence providing better performance. •  It’s suitable for cases where there are large number of nodes but only few of them are accessed frequently. https://www.cs.usfca.edu/~galles/visualization/SplayTree.html Browse the link below for visulalization of Splay tree
  • 21. 28-02-2020 21 # A O(n^2) Python3 program for construction of BST from preorder traversal # A binary tree node class Node(): # A constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None # constructTreeUtil.preIndex is a static variable of # function constructTreeUtil # Function to get the value of static variable # constructTreeUtil.preIndex def getPreIndex(): return constructTreeUtil.preIndex # Function to increment the value of static variable # constructTreeUtil.preIndex def incrementPreIndex(): constructTreeUtil.preIndex += 1 # A recurseive function to construct Full from pre[]. # preIndex is used to keep track of index in pre[[]. def constructTreeUtil(pre, low, high, size): # Base Case if( getPreIndex() >= size or low > high): return None # The first node in preorder traversal is root. So take # the node at preIndex from pre[] and make it root, # and increment preIndex root = Node(pre[getPreIndex()]) incrementPreIndex() # If the current subarray has onlye one element, # no need to recur if low == high : return root # Search for the first element greater than root for i in range(low, high+1): if (pre[i] > root.data): break # Use the index of element found in preorder to divide # preorder array in two parts. Left subtree and right # subtree root.left = constructTreeUtil(pre, getPreIndex(), i-1 , size) root.right = constructTreeUtil(pre, i, high, size) return root # The main function to construct BST from given preorder # traversal. This function mailny uses constructTreeUtil() def constructTree(pre): size = len(pre) constructTreeUtil.preIndex = 0 return constructTreeUtil(pre, 0, size-1, size) def printInorder(root): if root is None: return printInorder(root.left) print root.data, printInorder(root.right) # Driver program to test above function pre = [10, 5, 1, 7, 40, 50] root = constructTree(pre) print "Inorder traversal of the constructed tree:" printInorder(root) Python code for Preorder BST
  • 22. Problem-1 •  Write a Python program to create a Balanced Binary Search Tree (BST) using an array (given) elements where array elements are sorted in ascending order. 28-02-2020 22 Online Python Compilter: http://pythontutor.com/live.html#mode=edit https://www.onlinegdb.com/online_python_debugger
  • 23. 28-02-2020 23 class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def sorted_array_to_bst(nums): if not nums: return None mid_val = len(nums)//2 node = TreeNode(nums[mid_val]) node.left = sorted_array_to_bst(nums[:mid_val ]) node.right = sorted_array_to_bst(nums[mid_val+ 1:]) return node def preOrder(node): if not node: return print(node.val) preOrder(node.left) preOrder(node.right) result = sorted_array_to_bst([1, 2, 3, 4, 5, 6, 7]) preOrder(result) Solution of the Problem 1
  • 24. Problem 2 •  Write a Python program to find the closest value of a given target value in a given non- empty Binary Search Tree (BST) of unique values. 28-02-2020 24 Online Python Compilter: http://pythontutor.com/live.html#mode=edit https://www.onlinegdb.com/online_python_debugger
  • 25. Solution of the Problem 2 28-02-2020 25 class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def closest_value(root, target): a = root.val kid = root.left if target < a else root.right if not kid: return a b = closest_value(kid, target) return min((a,b), key=lambda x: abs(target-x)) root = TreeNode(8) root.left = TreeNode(5) root.right = TreeNode(14) root.left.left = TreeNode(4) root.left.right = TreeNode(6) root.left.right.left = TreeNode(8) root.left.right.right = TreeNode(7) root.right.right = TreeNode(24) root.right.right.left = TreeNode(22) result = closest_value(root, 19) print(result)
  • 26. Problem 3 •  Write a Python program to check whether a given a binary tree is a valid binary search tree (BST) or not 28-02-2020 26 Let a binary search tree (BST) is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: 2 / 1 3 Binary tree [2,1,3], return true. Example 2: 1 / 2 3 Binary tree [1,2,3], return false.
  • 27. Problem 4 28-02-2020 27 •  Write a Python program to delete a node with the given key in a given Binary search tree (BST). Note: Search for a node to remove. If the node is found, delete the node.
  • 28. Solution to the Problem 4 28-02-2020 28 # Definition: Binary tree node. class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def delete_Node(root, key): # if root doesn't exist, just return it if not root: return root # Find the node in the left subtree if key value is less than root value if root.val > key: root.left = delete_Node(root.left, key) # Find the node in right subtree if key value is greater than root value, elif root.val < key: root.right= delete_Node(root.right, key) # Delete the node if root.value == key else: # If there is no right children delete the node and new root would be root.left if not root.right: return root.left # If there is no left children delete the node and new root would be root.right if not root.left: return root.right # If both left and right children exist in the node replace its value with # the minmimum value in the right subtree. Now delete that minimum node # in the right subtree temp_val = root.right mini_val = temp_val.val while temp_val.left: temp_val = temp_val.left mini_val = temp_val.val # Replace value root.val = mini # Delete the minimum node in right subtree root.right = deleteNode(root.right,root.val) return root def preOrder(node): if not node: return print(node.val) preOrder(node.left) preOrder(node.right) root = TreeNode(5) root.left = TreeNode(3) root.right = TreeNode(6) root.left.left = TreeNode(2) root.left.right = TreeNode(4) root.left.right.left = TreeNode(7) print("Original node:") print(preOrder(root)) result = delete_Node(root, 4) print("After deleting specified node:") print(preOrder(result))
  • 30. Useful Links •  http://www.csanimated.com/animation.php?t=Quicksort •  http://www.hakansozer.com/category/c/ •  http://www.nczonline.net/blog/2012/11/27/computer-science-in- javascript-quicksort/ •  http://www.sorting-algorithms.com/shell-sort •  https://www.tutorialspoint.com/ •  https://www.hackerearth.com/ •  www.khanacademy.org/computing/computer-science/algorithms •  https://www.w3resource.com/python-exercises/data-structures- and-algorithms/python-binary-search-tree-index.php#EDITOR •  https://medium.com/@stephenagrice/how-to-implement-a-binary- search-tree-in-python-e1cdba29c533 28-02-2020 30
  • 34. •  Given the following sequence of stack operations, what is the top item on the stack when the sequence is complete? 28-02-2020 34 m = Stack() m.push('x') m.push('y') m.pop() m.push('z') m.peek()
  • 35. •  Given the following sequence of stack operations, what is the top item on the stack when the sequence is complete? 28-02-2020 35 m = Stack() m.push('x') m.push('y') m.pop() m.push('z') m.peek()
  • 36. What this code is doing? 28-02-2020 36 stack = [] stack.append('a') stack.append('b') stack.append('c') print('Initial stack') print(stack) print('nElements poped from stack:') print(stack.pop()) print(stack.pop()) print(stack.pop()) print('nStack after elements are poped:') print(stack)
  • 37. Stack implementation code 28-02-2020 37 stack = [] stack.append('a') stack.append('b') stack.append('c') print('Initial stack') print(stack) print('nElements poped from stack:') print(stack.pop()) print(stack.pop()) print(stack.pop()) print('nStack after elements are poped:') print(stack) OUTPUT Initial stack ['a', 'b', 'c'] Elements poped from stack: c b a Stack after elements are poped: []
  • 38. What this code is doing? 28-02-2020 38 queue = [] queue.append('a') queue.append('b') queue.append('c') print("Initial queue") print(queue) print("nElements dequeued from queue") print(queue.pop(0)) print(queue.pop(0)) print(queue.pop(0)) print("nQueue after removing elements") print(queue)
  • 39. What this code is doing? 28-02-2020 39 queue = [] queue.append('a') queue.append('b') queue.append('c') print("Initial queue") print(queue) print("nElements dequeued from queue") print(queue.pop(0)) print(queue.pop(0)) print(queue.pop(0)) print("nQueue after removing elements") print(queue) Output Initial queue ['a', 'b', 'c'] Elements dequeued from queue a b c Queue after removing elements []
  • 40. Tree •  A Tree is a collection of elements called nodes. •  One of the node is distinguished as a root, along with a relation (“parenthood”) that places a hierarchical structure on the nodes. 28-02-2020 40
  • 41. Applications of Tree 28-02-2020 41 Main applications of trees include: 1. Manipulate hierarchical data. 2. Make information easy to search (see tree traversal). 3. Manipulate sorted lists of data. 4. As a workflow for compositing digital images for visual effects. 5. Router algorithms 6. Form of a multi-stage decision-making (see business chess).
  • 42. Binary Tree 28-02-2020 42 •  A tree whose elements have at most 2 children is called a binary tree. •  Since each element in a binary tree can have only 2 children, we typically name the left child and right child.
  • 43. Binary Tree 28-02-2020 43 •  A tree whose elements have at most 2 children is called a binary tree. •  Since each element in a binary tree can have only 2 children, we typically name the left child and right child. ((7+3)∗(5−2))
  • 44. Binary Tree 28-02-2020 44 •  A tree whose elements have at most 2 children is called a binary tree. •  Since each element in a binary tree can have only 2 children, we typically name the left child and right child. •  A Binary Tree node contains following parts. •  Data •  Pointer to left child •  Pointer to right child
  • 45. Python code of Binary Tree 28-02-2020 45 # A class that represents an individual node in a # Binary Tree class Node: def __init__(self,key): self.left = None self.right = None self.val = key # create root root = Node(1) ''' following is the tree after above statement 1 / None None''' root.left = Node(2); root.right = Node(3); ''' 2 and 3 become left and right children of 1 1 / 2 3 / / None None None None''’ root.left.left = Node(4); '''4 becomes left child of 2 1 / 2 3 / / 4 None None None / None None''' "__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.
  • 46. Tree traversal 28-02-2020 46 Following are the generally used ways for traversing trees: •  Inorder: 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. •  Preorder: 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. •  Postorder: We recursively do a postorder traversal of the left subtree and the right subtree followed by a visit to the root node.
  • 47. Tree traversal 28-02-2020 47 (a) Inorder (Left, Root, Right) : 4 2 5 1 3 (b) Preorder (Root, Left, Right) : 1 2 4 5 3 (c) Postorder (Left, Right, Root) : 4 5 2 3 1 Level Order Traversal : 1 2 3 4 5
  • 48. Tree traversal 28-02-2020 48 (a) Inorder (Left, Root, Right) : 4 2 5 1 3 (b) Preorder (Root, Left, Right) : 1 2 4 5 3 (c) Postorder (Left, Right, Root) : 4 5 2 3 1 Level Order Traversal : 1 2 3 4 5
  • 49. Tree traversal code 28-02-2020 49 # A class that represents an individual node in a Binary Tree class Node: def __init__(self,key): self.left = None self.right = None self.val = key # A function to do inorder tree traversal def printInorder(root): if root: # First recur on left child printInorder(root.left) # then print the data of node print(root.val), # now recur on right child printInorder(root.right) # A function to do postorder tree traversal def printPostorder(root): if root: # First recur on left child printPostorder(root.left) # the recur on right child printPostorder(root.right) # now print the data of node print(root.val), # A function to do preorder tree traversal def printPreorder(root): if root: # First print the data of node print(root.val), # Then recur on left child printPreorder(root.left) # Finally recur on right child printPreorder(root.right) # Driver code root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) print ("nPreorder traversal of binary tree is") printPreorder(root) print ("nInorder traversal of binary tree is") printInorder(root) print ("nPostorder traversal of binary tree is") printPostorder(root)
  • 50. Inorder traversal without recursion 28-02-2020 50 # A binary tree node class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None # Iterative function for inorder tree traversal def inOrder(root): # Set current to root of binary tree current = root stack = [] # initialize stack done = 0 while True: # Reach the left most Node of the current Node if current is not None: # Place pointer to a tree node on the stack # before traversing the node's left subtree stack.append(current) current = current.left # BackTrack from the empty subtree and visit the Nod # at the top of the stack; however, if the stack is # empty you are done elif(stack): current = stack.pop() print(current.data, end=" ") # Python 3 printing # We have visited the node and its left # subtree. Now, it's right subtree's turn current = current.right else: break print() # Driver program to test above function """ Constructed binary tree is 1 / 2 3 / 4 5 """ root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) inOrder(root)
  • 51. Priority Queue 28-02-2020 51 •  A priority queue acts like a queue in that you dequeue an item by removing it from the front. •  In a priority queue the logical order of items inside a queue is determined by their priority. •  The highest priority items are at the front of the queue and the lowest priority items are at the back. •  When you enqueue an item on a priority queue, the new item may move all the way to the front.
  • 52. Binary Heaps 28-02-2020 52 •  A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Generally, Heaps can be of two types: •  Max-Heap: The key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. •  Min-Heap: The key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
  • 53. Binary Heap Operation 28-02-2020 53 The basic operations we will implement for our binary heap are as follows: •  BinaryHeap() creates a new, empty, binary heap. •  insert(k) adds a new item to the heap. •  findMin() returns the item with the minimum key value, leaving item in the heap. •  delMin() returns the item with the minimum key value, removing the item from the heap. •  isEmpty() returns true if the heap is empty, false otherwise. •  size() returns the number of items in the heap. •  buildHeap(list) builds a new heap from a list of keys.