spsl v unit - final
TRANSCRIPT
![Page 1: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/1.jpg)
Shell Programming & Scripting Languages
Dr. K. Sasidhar
![Page 2: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/2.jpg)
UNIT – VI Contents Object-Orientation
Data in Python, Data Structures in Python, Defining Classes
The Python Database Interface. Database Interfaces, The Underlying Interface Model,
Some Database Modules, A Simple Database-Driven Web, SQL/Python Communication.
![Page 3: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/3.jpg)
Unit VI outcomes From the VI unit Student can
Implement user defined type class. Design oops concepts like inheritance. Understand, learn Python interface with databases. Connect with database and can write solution as a python
program and executes.
![Page 4: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/4.jpg)
Object Oriented Programming in Python Class: A user-defined prototype for an object that defines
a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
Object: Instance of a class. An object comprises both data members (class variables and instance variables) and methods.
![Page 5: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/5.jpg)
OOP in Python: Defining a class A class is a special data type which defines how to build a
certain kind of object. The class also stores some data items that are shared by all the
instances of this class Instances are objects that are created which follow the
definition given inside of the class Python doesn’t use separate class interface definitions as in
some languages You just define the class and then use it
![Page 6: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/6.jpg)
Defining Methods Define a method in a class by including function definitions within the
scope of the class block There must be a special first argument self in all method definitions which
gets bound to the calling instance There is usually a special method called __init__ in most classes There is no “new” keyword in python Use the class name with ( ) notation and assign the result to a
variable __init__ serves as a constructor for the class. Usually does some
initialization work The arguments passed to the class name are given to its __init__()
method So, the __init__ method for employee is passed “Bob” and 15000
and the new class instance is bound to b: b=Employee(“Bob”,15000)
![Page 7: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/7.jpg)
Simple class example class shareddata(object): a=44
x=shareddata() y=shareddata() z=shareddata()
print(x.a) print(y.a) print(z.a)
# Creates three instances
# Inherit and share a
![Page 8: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/8.jpg)
Creating classes class ClassName:
class variables methods # methods are functions inside a classExample:
class Employee: empCount = 0 def __init__(self, name, salary): #__init__ runs when a new instance
object is created. #self is an
instance self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self):
print(“count”, self.empCount) def displayDetails(self): print( "Name : ", self.name, ", Salary: ", self.salary )
![Page 9: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/9.jpg)
Constructor: __init__
An __init__ method can take any number of arguments.
Like other functions or methods, the arguments can be defined with default values, making them optional to the caller.
However, the first argument self in the definition of __init__ is special…
![Page 10: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/10.jpg)
Program to update the employee salary.
class Person: def __init__(self, name, job=None, pay=0): self.name = name self.job = job self.pay = pay def lastName(self): # Behavior methods return self.name.split()[-1] # self is implied
subject def giveRaise(self, percent): self.pay = int(self.pay * (1 + percent)) # Must change
here only if __name__ == '__main__': bob = Person(' Smith') sue = Person(' Jones', job='dev', pay=10000) print(bob.name, bob.pay) print(sue.name, sue.pay) print(bob.lastName(), sue.lastName()) # Use the new
methods sue.giveRaise(.10) # instead of hardcoding print(sue.pay)
![Page 11: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/11.jpg)
Inheritance
![Page 12: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/12.jpg)
Subclasses A class can extend the definition of another class
Allows use (or extension ) of methods and attributes already defined in the previous one.
To define a subclass, put the name of the superclass in parentheses after the subclass’s name on the first line of the definition. Class ECM_student(student):Python has no ‘extends’ keyword like Java.Multiple inheritance is supported.
![Page 13: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/13.jpg)
Redefining Methods
To redefine a method of the parent class, include a new definition using the same name in the subclass. The old code won’t get executed.
To execute the method in the parent class in addition to new code for some method, explicitly call the parent’s version of the method.parentClass.methodName(self, a, b, c) The only time you ever explicitly pass ‘self’ as an argument is
when calling a method of an ancestor.
![Page 14: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/14.jpg)
Definition of a class extending student
Class Student: #“A class representing a student.”def __init__(self,n,a): self.full_name = n self.age = a
def get_age(self): return self.age
Class ECM_student (student): #class extending student.”
def __init__(self,n,a,s): student.__init__(self,n,a) #Call __init__ for student self.section_num = s
def get_age(): #Redefines get_age method entirely print “Age: ” + str(self.age)
![Page 15: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/15.jpg)
Database Programming The Python standard for database interfaces is the Python DB-
API. Python Database API supports a wide range of database servers
such as: MySQL PostgreSQL Microsoft SQL Server 2000 Informix Interbase Oracle Sybase GadFly mSQL
![Page 16: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/16.jpg)
Working with Databases through Python Download a separate DB API module for each database
you need to access. For example, to access an Oracle database as well as a
MySQL database, download both the Oracle and the MySQL database modules.
![Page 17: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/17.jpg)
Database connectivity and display version import MySQLdb # Open database connection db =
MySQLdb.connect("localhost","testuser","test123","TESTDB" )
# prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. cursor.execute("SELECT VERSION()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print "Database version : %s " % data # disconnect from server db.close()
![Page 18: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/18.jpg)
Creating a Table Using python import MySQLdb # Open database connection db =
MySQLdb.connect("localhost","testuser","test123","TESTDB" )
# prepare a cursor object using cursor() method cursor = db.cursor() # Drop table if it already exist using execute() method. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # Create table as per requirement sql = """CREATE TABLE EMPLOYEE ( empid Number(20) NOT NULL, NAME CHAR(20), Job char(10), Sal number(10)) cursor.execute(sql) db.close
![Page 19: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/19.jpg)
READ Operation
READ Operation on any database means to fetch some useful information from the database.
Once database connection is established, then we can send a query into this database.
use either fetchone() method to fetch single record. fetchall() method to fetch multiple values from a database
table. fetchone(): It fetches the next row of a query result set.
A result set is an object that is returned when a cursor object is used to query a table.
fetchall(): It fetches all the rows in a result set. If some rows have already been extracted from the result set, then it retrieves the remaining rows from the result set.
rowcount: This is a read-only attribute and returns the number of rows that were affected by an execute() method.
![Page 20: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/20.jpg)
Program to display all the records from EMPLOYEE table having salary more than 1000:
import MySQLdb db =
MySQLdb.connect("localhost","testuser","test123","TESTDB" )
# prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000) try: cursor.execute(sql)
![Page 21: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/21.jpg)
results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=
%d" % \ (fname, lname, age, sex, income ) except: print "Error: unable to fecth data" db.close()
![Page 22: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/22.jpg)
Python Data Structures
![Page 23: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/23.jpg)
Lists An ordered group of items Does not need to be the same type
Could put numbers, strings or donkeys in the same list
List notation A = [1,”This is a list”, c, Donkey(“kong”)]
![Page 24: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/24.jpg)
Methods of Lists
List.append(x) adds an item to the end of the list
List.extend(L) Extend the list by appending all in the given list L
List.insert(I,x) Inserts an item at index I
List.remove(x) Removes the first item from the list whose value is
x
![Page 25: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/25.jpg)
Examples of other methods a = [66.25, 333, 333, 1, 1234.5] //Defines List
print a.count(333), a.count(66.25), a.count('x') //calls method
2 1 0 //output a.index(333)
//Returns the first index where the given value appears
1 //ouput a.reverse() //Reverses order of list
a //Prints list a [333, 1234.5, 1, 333, -1, 66.25] //Ouput
a.sort() a //Prints list a [-1, 1, 66.25, 333, 333, 1234.5] //Output
![Page 26: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/26.jpg)
Using Lists as Stacks The last element added is the first element retrieved To add an item to the stack,
append() must be used stack = [3, 4, 5] stack.append(6) Stack is now [3, 4, 5, 6]
To retrieve an item from the top of the stack, pop must be used Stack.pop() 6 is output Stack is now [3, 4, 5] again
![Page 27: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/27.jpg)
Using Lists as Queues
First element added is the first element retrieved To do this collections.deque
must be implemented
![Page 28: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/28.jpg)
List Programming Tools
Filter(function, sequence) Returns a sequence consisting of the items from the sequence
for which function(item) is true
Computes primes up to 25
![Page 29: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/29.jpg)
Map Function Map(function, sequence)
Calls function(item) for each of the sequence’s items
Computes the cube for the range of 1 to 11
![Page 30: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/30.jpg)
Reduce Function
Reduce(function, sequence) Returns a single value constructed by calling the binary
function (function)
Computes the sum of the numbers 1 to 10
![Page 31: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/31.jpg)
The del statement A specific index or range can be deleted
![Page 32: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/32.jpg)
Tuples Tuple
A number of values separated by commas Immutable
Cannot assign values to individual items of a tuple However tuples can contain mutable objects such as lists
Single items must be defined using a comma Singleton = ‘hello’,
![Page 33: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/33.jpg)
Sets An unordered collection with no duplicate elements Basket = [‘apple’, ‘orange’, ‘apple’, ‘pear’] Fruit = set(basket) Fruit
Set([‘orange’, ‘apple’, ‘pear’])
![Page 34: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/34.jpg)
Dictionaries Indexed by keys
This can be any immutable type (strings, numbers…) Tuples can be used if they contain only immutable
objects
![Page 35: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/35.jpg)
Looping Techniques Iteritems():
for retrieving key and values through a dictionary
![Page 36: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/36.jpg)
Looping Techniques Enumerate():
for the position index and values in a sequence
![Page 37: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/37.jpg)
Zip(): for looping over two or more sequences
![Page 38: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/38.jpg)
Comparisons Operators “in” and “not in” can be used to see
if an item exists in a sequence Comparisons can be chained
a < b == c This tests whether a is less than b and that b equals c
![Page 39: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/39.jpg)
Fundamentals of Python: From First Programs Through Data Structures 39
Tree Terminology
![Page 40: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/40.jpg)
Fundamentals of Python: From First Programs Through Data Structures 40
Tree Terminology (continued)
![Page 41: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/41.jpg)
Fundamentals of Python: From First Programs Through Data Structures 41
Note: The height of a tree containing one node is 0By convention, the height of an empty tree is –1
Tree Terminology (continued)
![Page 42: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/42.jpg)
Fundamentals of Python: From First Programs Through Data Structures 42
General Trees and Binary Trees In a binary tree, each node has at most two
children: The left child and the right child
![Page 43: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/43.jpg)
Fundamentals of Python: From First Programs Through Data Structures 43
Recursive Definitions of Trees A general tree is either empty or consists of a finite set of
nodes T Node r is called the root The set T – {r} is partitioned into disjoint subsets, each of
which is a general tree A binary tree is either empty or consists of a root plus a left
subtree and a right subtree, each of which are binary trees
![Page 44: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/44.jpg)
Fundamentals of Python: From First Programs Through Data Structures 44
Why Use a Tree? A parse tree describes the syntactic
structure of a particular sentence in terms of its component parts
![Page 45: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/45.jpg)
Fundamentals of Python: From First Programs Through Data Structures 45
Why Use a Tree? (continued) File system structures are also tree-like
![Page 46: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/46.jpg)
Fundamentals of Python: From First Programs Through Data Structures 46
Why Use a Tree? (continued) Sorted collections can also be represented as
tree-like structures Called a binary search tree, or BST for short
Can support logarithmic searches and insertions
![Page 47: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/47.jpg)
Fundamentals of Python: From First Programs Through Data Structures 47
The Shape of Binary Trees The shape of a binary tree can be described
more formally by specifying the relationship between its height and the number of nodes contained in it
N nodesHeight: N – 1
A full binary tree contains the maximum number of nodes for a givenheight H
![Page 48: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/48.jpg)
Fundamentals of Python: From First Programs Through Data Structures 48
The Shape of Binary Trees (continued)
The number of nodes, N, contained in a full binary tree of height H is 2H + 1 – 1
The height, H, of a full binary tree with N nodes is log2(N + 1) – 1
The maximum amount of work that it takes to access a given node in a full binary tree is O(log N)
![Page 49: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/49.jpg)
Fundamentals of Python: From First Programs Through Data Structures 49
The Shape of Binary Trees (continued)
![Page 50: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/50.jpg)
Fundamentals of Python: From First Programs Through Data Structures 50
Three Common Applications of Binary Trees In this section, we introduce three special
uses of binary trees that impose an ordering on their data: Heaps Binary search trees Expression trees
![Page 51: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/51.jpg)
Fundamentals of Python: From First Programs Through Data Structures 51
A Binary Tree ADT Provides many common operations required
for building more specialized types of trees Should support basic operations for creating
trees, determining if a tree is empty, and traversing a tree
Remaining operations focus on accessing, replacing, or removing the component parts of a nonempty binary tree—its root, left subtree, and right subtree
![Page 52: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/52.jpg)
Fundamentals of Python: From First Programs Through Data Structures 52
The Interface for a Binary Tree ADT
![Page 53: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/53.jpg)
Fundamentals of Python: From First Programs Through Data Structures 53
The Interface for a Binary Tree ADT (continued)
![Page 54: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/54.jpg)
Fundamentals of Python: From First Programs Through Data Structures 54
Processing a Binary Tree
Many algorithms for processing binary trees follow the trees’ recursive structure
Programmers are occasionally interested in the frontier, or set of leaf nodes, of a tree Example: Frontier of parse tree for English sentence shown
earlier contains the words in the sentence
![Page 55: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/55.jpg)
Fundamentals of Python: From First Programs Through Data Structures 55
Processing a Binary Tree (continued)
frontier expects a binary tree and returns a list Two base cases:
Tree is empty return an empty list Tree is a leaf node return a list containing root item
![Page 56: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/56.jpg)
Fundamentals of Python: From First Programs Through Data Structures 56
Implementing a Binary Tree
![Page 57: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/57.jpg)
Fundamentals of Python: From First Programs Through Data Structures 57
Implementing a Binary Tree (continued)
![Page 58: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/58.jpg)
Fundamentals of Python: From First Programs Through Data Structures 58
Implementing a Binary Tree (continued)
![Page 59: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/59.jpg)
Fundamentals of Python: From First Programs Through Data Structures 59
The String Representation of a Tree
__str__ can be implemented with any of the traversals
![Page 60: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/60.jpg)
Fundamentals of Python: From First Programs Through Data Structures 60
Developing a Binary Search Tree A BST imposes a special ordering on the
nodes in a binary tree, so as to support logarithmic searches and insertions
In this section, we use the binary tree ADT to develop a binary search tree, and assess its performance
![Page 61: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/61.jpg)
Fundamentals of Python: From First Programs Through Data Structures 61
The Binary Search Tree Interface The interface for a BST should include a constructor and
basic methods to test a tree for emptiness, determine the number of items, add an item, remove an item, and search for an item
Another useful method is __iter__, which allows users to traverse the items in BST with a for loop
![Page 62: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/62.jpg)
Fundamentals of Python: From First Programs Through Data Structures 62
Data Structures for the Implementation of BST
…
![Page 63: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/63.jpg)
Fundamentals of Python: From First Programs Through Data Structures 63
Searching a Binary Search Tree find returns the first matching item if the target item is in
the tree; otherwise, it returns None We can use a recursive strategy
![Page 64: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/64.jpg)
Fundamentals of Python: From First Programs Through Data Structures 64
Inserting an Item into a Binary Search Tree add inserts an item in its proper place in the BST Item’s proper place will be in one of three positions:
The root node, if the tree is already empty A node in the current node’s left subtree, if new item is less
than item in current node A node in the current node’s right subtree, if new item is
greater than or equal to item in current node For options 2 and 3, add uses a recursive helper function
named addHelper In all cases, an item is added as a leaf node
![Page 65: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/65.jpg)
Fundamentals of Python: From First Programs Through Data Structures 65
Removing an Item from a Binary Search Tree Save a reference to root node Locate node to be removed, its parent, and its parent’s
reference to this node If item is not in tree, return None Otherwise, if node has a left and right child, replace
node’s value with largest value in left subtree and delete that value’s node from left subtree Otherwise, set parent’s reference to node to node’s only child
Reset root node to saved reference Decrement size and return item
![Page 66: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/66.jpg)
Fundamentals of Python: From First Programs Through Data Structures 66
Removing an Item from a Binary Search Tree (continued) Fourth step is fairly complex: Can be factored out into a
helper function, which takes node to be deleted as a parameter (node containing item to be removed is referred to as the top node): Search top node’s left subtree for node containing the largest
item (rightmost node of the subtree) Replace top node’s value with the item If top node’s left child contained the largest item, set top node’s
left child to its left child’s left child Otherwise, set parent node’s right child to that right child’s left
child
![Page 67: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/67.jpg)
Fundamentals of Python: From First Programs Through Data Structures 67
Complexity Analysis of Binary Search Trees BSTs are set up with intent of replicating O(log n)
behavior for the binary search of a sorted list A BST can also provide fast insertions Optimal behavior depends on height of tree
A perfectly balanced tree supports logarithmic searches Worst case (items are inserted in sorted order): tree’s height is
linear, as is its search behavior
Insertions in random order result in a tree with close-to-optimal search behavior
![Page 68: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/68.jpg)
Fundamentals of Python: From First Programs Through Data Structures 68
Case Study: Parsing and Expression Trees Request:
Write a program that uses an expression tree to evaluate expressions or convert them to alternative forms
Analysis: Like the parser developed in Chapter 17, current program
parses an input expression and prints syntax error messages if errors occur
If expression is syntactically correct, program prints its value and its prefix, infix, and postfix representations
![Page 69: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/69.jpg)
Fundamentals of Python: From First Programs Through Data Structures 69
Case Study: Parsing and Expression Trees (continued)
![Page 70: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/70.jpg)
Fundamentals of Python: From First Programs Through Data Structures 70
Case Study: Parsing and Expression Trees (continued)
Design and Implementation of the Node Classes:
![Page 71: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/71.jpg)
Fundamentals of Python: From First Programs Through Data Structures 71
Case Study: Parsing and Expression Trees (continued)
![Page 72: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/72.jpg)
Fundamentals of Python: From First Programs Through Data Structures 72
Case Study: Parsing and Expression Trees (continued)
![Page 73: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/73.jpg)
Fundamentals of Python: From First Programs Through Data Structures 73
Case Study: Parsing and Expression Trees (continued)
Design and Implementation of the Parser Class: Easiest to build an expression tree with a parser that uses a
recursive descent strategy parse should now return an expression tree to its caller,
which uses that tree to obtain information about the expression
factor processes either a number or an expression nested in parentheses Calls expression to parse nested expressions
![Page 74: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/74.jpg)
Fundamentals of Python: From First Programs Through Data Structures 74
Case Study: Parsing and Expression Trees (continued)
![Page 75: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/75.jpg)
Fundamentals of Python: From First Programs Through Data Structures 75
Case Study: Parsing and Expression Trees (continued)
![Page 76: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/76.jpg)
Fundamentals of Python: From First Programs Through Data Structures 76
An Array Implementation of Binary Trees An array-based implementation of a binary
tree is difficult to define and practical only in some cases
For complete binary trees, there is an elegant and efficient array-based representation Elements are stored by level
The array representation of a binary tree is pretty rare and is used mainly to implement a heap
![Page 77: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/77.jpg)
Fundamentals of Python: From First Programs Through Data Structures 77
An Array Implementation of Binary Trees (continued)
![Page 78: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/78.jpg)
Fundamentals of Python: From First Programs Through Data Structures 78
An Array Implementation of Binary Trees (continued)
![Page 79: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/79.jpg)
Fundamentals of Python: From First Programs Through Data Structures 79
An Array Implementation of Binary Trees (continued)
![Page 80: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/80.jpg)
Fundamentals of Python: From First Programs Through Data Structures 80
Implementing Heaps
![Page 81: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/81.jpg)
Fundamentals of Python: From First Programs Through Data Structures 81
Implementing Heaps (continued)
At most, log2n comparisons must be made to walk up the tree from the bottom, so add is O(log n)
Method may trigger a doubling in the array size O(n), but amortized over all additions, it is O(1)
![Page 82: Spsl v unit - final](https://reader034.vdocuments.site/reader034/viewer/2022042707/58edf1b01a28ab800e8b4575/html5/thumbnails/82.jpg)
Fundamentals of Python: From First Programs Through Data Structures 82
Using a Heap to Implement a Priority Queue we can use a heap