Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

look at the class below and then provide code for the functions that has pass written in them import random import sys from typing import

look at the class below and then provide code for the functions that has pass written in them

import random

import sys

from typing import Any, Optional

class Node(object):

'''A node in a skiplist. It stores a (key, value) pair along with pointers

for each level in its tower.

The key is used to compare nodes. The tower automatically includes level 0.

'''

def __init__(self, data: (Any,Any), height: int=0) -> None:

self.data = data

self.tower = [None] * (height + 1)

def __repr__(self) -> str:

return f'Node({self.data}, {len(self.tower)})'

def __str__(self) -> str:

return self.__repr__()

def height(self) -> int:

return len(self.tower) - 1

def key(self) -> Any:

return self.data[0]

def value(self) -> Any:

return self.data[1]

def add_level(self, forward: Optional[Node] = None) -> None:

'''Adds a level to this node which points to forward.""

self.tower.append(forward)

class SkipList(object):

'''A skiplist of nodes containing (key, value) pairs. Nodes are ordered

according to keys. Keys are unique, reinserting an existing key overwrites

the value.

The skiplist contains a sentinel node by default and the height of the

sentinel node is the height of the list.

'''

def __init__(self) -> None:

self.sentinel = Node((None, None), sys.maxsize)

self.size = 0

self.height = 1 #lowest height

def __len__(self) -> int:

'''Returns the number of pairs stored in this skiplist.

dunder method allows calling len() on this object""

return self.size

def __repr__(self) -> str:

'''Returns a string representation of this skiplist.

Implement any representation that helps you debug.

Parameters:

- self: mandatory reference to this object

Returns:

this skiplist's string representation.

'''

return f'SkipList({self.size})'

def __str__(self) -> str:

'''Returns a string representation of this skiplist.

See the link below for the difference between the __repr__ and __str__

methods: https://www.geeksforgeeks.org/str-vs-repr-in-python/

Parameters:

- self: mandatory reference to this object

Returns:

this skiplist's string representation.

'''

return self.__repr__()

def _search_path(self, key: Any) -> [Node]:

'''Returns the search path in this skiplist for key.

The search path contains one node for each level of the skiplist

starting from the highest and ending at level 0. The node for each

level is the one corresponding to a descend in the search path.

Parameters:

- self: mandatory reference to this object

- key: the key being searched for

Returns:

the descend nodes at each level of the skiplist, ordered from highest

level to level 0.

'''

path = [None] * (self.height + 1)

current = self.sentinel #search from heightest level

for i in range(self.height):

while current.tower[i] < key:

current = current.tower[i]

path[i] = current

return path

def _find_prev(self, key: Any) -> Node:

'''Returns the node in the skiplist that contains the predecessor key.

Parameters:

- self: mandatory reference to this object

- key: the key being searched for

Returns:

the node in the skiplist that contains the predecessor key.

'''

path = self._search_path(key)

if path[0].tower[0] == key: # check if key is already in the list

return path[0].tower[0]

return path[0]

def reset(self) -> None:

'''Empty the skiplist.

Parameters:

- self: mandatory reference to this object

Returns:

None

'''

pass

def height(self) -> int:

'''Returns the height of the skiplist.

The height is the largest level of the sentinel's tower.

Parameters:

- self: mandatory reference to this object

Returns:

the height of this skiplist.

'''

pass

def find(self, key: Any) -> Optional[Any]:

'''Returns the value stored in this skiplist corresponding to key, None

if key does not exist in this skiplist.

Parameters:

- self: mandatory reference to this object

- key: the key whose value is sought

Returns:

the stored value for key, None if key does not exist in this skiplist.

'''

pass

def find_range(self, key1: Any, key2: Any) -> [Any]:

'''Returns the values stored in this skiplist corresponding to the keys

between key1 and key2 inclusive in sorted order of keys.

Parameters:

- self: mandatory reference to this object

- key1: starting key in the range of keys whose value is sought

- key2: ending key in the range of keys whose value is sought

Returns:

the stored values for the keys between key1 and key2 inclusive in sorted

order of keys.

'''

pass

def remove(self, key: Any) -> Optional[Any]:

'''Returns the value stored for key in this skiplist and removes

(key, value) from this skiplist, returns None if key is not stored in

this skiplist.

Parameters:

- self: mandatory reference to this object

- key: the key to be removed

Returns:

the stored value for key in this skiplist, None if key does not exist

in this skiplist

'''

pass

def insert(self, data: (Any,Any)) -> None:

'''Inserts a (key value) pair in this skiplist, overwrites the old value

if key already exists.

Parameters:

- self: mandatory reference to this object

- data: the (key, value) pair

Returns:

None

'''

pass

def size(self) -> int:

'''Returns the number of pairs stored in this skiplist.

Parameters:

- self: mandatory reference to this object

Returns:

the number of pairs stored in this skiplist.

'''

pass

def is_empty(self) -> bool:

'''Returns whether the skiplist is empty.

Parameters:

- self: mandatory reference to this object

Returns:

True if no pairs are stored in this skiplist, False otherwise.

'''

pass

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions

Question

Distinguish between demotions, transfers, and promotions.

Answered: 1 week ago