Skip to main content

This python package creates and manages the common data structures like queue, priority queue, stack, linkedlist, circular linkedlist, binary tree, set etc. Supported data types are int, float, str, bool, NoneType, list, tuple, and dictionary.

Project description

pcds-1.1.2 (Python Common Data Structures)

Description: This python package contains some basic and common data structures like stack, queue, priority queue, singly linkedlist, doubly linkedlist, circular linkedlist, binary search tree. and set data structures. Now pcds can store data of the following types: integer, float, string, boolean, nonetype, list, tuple, and dictionary. The data structure classes can save the data in the hard disk both in encrypted and in plain text form. Encrypted data serves the purpose of data security. To maintain data integrity, data are saved with the md5 hash code and data are ckecked with the hash code while reading from the disk. If data is corrupt, data file will not be opened. Thus data security and integrity both are maintained by pcds.

Default data file directory: home + "\\pcds_data"

Default data file extension: .pcds (python common data structure)

Operations on data structures are very fast, reliable and user friendly. So, using this package will be a great experience to the python programmers.

Quick Tutorial

Stack Data Structure

Stack is basically a LIFO (Last In First Out) type data structure. Data are entered by push() method and the top most data is accessed by pop() method.

Default data file address: home+"\\pcds_data\\stack.pcds"

Example Code

>>> 
>>> from pcds import stack as stk
>>> stk1=stk.Stack()
>>> stk1.push(10)
>>> stk1.push(20)
>>> stk1.push(14.78)
>>> stk1.push(True)
>>> stk1.push(False)
>>> stk1.push(None)
>>> stk1.push('hello')
>>> stk1.push(['abc',2,4,5,8.9])
>>> stk1.push(('world',2,[4,5,8],9))
>>> stk1.push({'name':'audry','age':20,'marks':[14,15,18]})
>>> stk1.push(77)
>>>
>>> stk1
<Stack[size=11] Bottom:(10, 20, 14.78, True, False, None, 'hello', ['abc', 2, 4, 5, 8.9], ('world', 2, [4, 5, 8], 9), {'name': 'audry', 'age': 20, 'marks': [14, 15, 18]}, 77):Top>
>>>
>>> stk1.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\stack.pcds'
True
>>>
>>> stk1.pop()
77
>>> stk1.pop()
{'name': 'audry', 'age': 20, 'marks': [14, 15, 18]}
>>> stk1.pop()
('world', 2, [4, 5, 8], 9)
>>> stk1.pop()
['abc', 2, 4, 5, 8.9]
>>> stk1.pop()
'hello'
>>>
>>> stk1
<Stack[size=6] Bottom:(10, 20, 14.78, True, False, None):Top>
>>>
>>> stk1.clear()
>>>
>>> stk1
<Stack[size=0] Bottom:():Top>
>>>
>>> stk1.loadDataFromFile()
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\stack.pcds'
True
>>>
>>> stk1
<Stack[size=11] Bottom:(10, 20, 14.78, True, False, None, 'hello', ['abc', 2, 4, 5, 8.9], ('world', 2, [4, 5, 8], 9), {'name': 'audry', 'age': 20, 'marks': [14, 15, 18]}, 77):Top>
>>>

copy() method copies the stack structure to a new instance

>>>
>>> from pcds import stack as stk
>>> s1=stk.Stack()
>>> s1.push(50)
>>> s1.push(12.89)
>>> s1.push(True)
>>> s1.push(False)
>>> s1.push(None)
>>> s1.push([1,2,3])
>>> s1
<Stack[size=6] Bottom:(50, 12.89, True, False, None, [1, 2, 3]):Top>
>>>
>>> s2=s1.copy()
>>> s2
<Stack[size=6] Bottom:(50, 12.89, True, False, None, [1, 2, 3]):Top>
>>> s2.pop()
[1, 2, 3]
>>> s2.push({1:'ebook'})
>>> s2.push((7,8,9))
>>> s2
<Stack[size=7] Bottom:(50, 12.89, True, False, None, {1: 'ebook'}, (7, 8, 9)):Top>
>>> s2.pop()
(7, 8, 9)
>>> s2
<Stack[size=6] Bottom:(50, 12.89, True, False, None, {1: 'ebook'}):Top>
>>>

Queue Data Structure

Queue is a FIFO (First In First Out) type data structure. The data which gets in the queue first, will be get out first. Data is entered by enQueue() method and is accessed by deQueue() method. The oldest data in the queue is called the head. When the data are accessed, the head will be dequeued first. Queue has a finite length. Default size is 100.

The size of the queue is to be set when the queue instance is created. When the data are entered in the queue, if queue is full, the head will be dequed automatically.

Queue data can be saved by save() method at any time either in encrypted or in plain text form.

Default data file address: home+"\\pcds_data\\queue.pcds"

Example Code

>>> 
>>> from pcds import queue as q
>>> q1=q.Queue()
>>> q1.enQueue(10)
>>> q1.enQueue(30)
>>> q1.enQueue(40.87)
>>> q1.enQueue('hello')
>>> q1.enQueue(True)
>>> q1.enQueue(False)
>>> q1.enQueue(None)
>>> q1.enQueue(0)
>>> q1.enQueue([1,3,5,'a','b','c',47])
>>> q1.enQueue((1,5,8,'world',[4,7,8,{'name':'ali'}]))
>>> q1.enQueue('world')
>>>
>>> q1
<Queue [11/100], Head:(10, 30, 40.87, 'hello', True, False, None, 0, [1, 3, 5, 'a', 'b', 'c', 47], (1, 5, 8, 'world', [4, 7, 8, {'name': 'ali'}]), 'world'):Tail>
>>>
>>> q1.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\queue.pcds'
True
>>>
>>> q1.clear()
>>>
>>> q1
<Queue [0/100], Head:():Tail>
>>>
>>> q1.loadDataFromFile()
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\queue.pcds'
True
>>>
>>> q1
<Queue [11/100], Head:(10, 30, 40.87, 'hello', True, False, None, 0, [1, 3, 5, 'a', 'b', 'c', 47], (1, 5, 8, 'world', [4, 7, 8, {'name': 'ali'}]), 'world'):Tail>
>>>

Creating a queue of length 5

Queue will be full if there are five data in the queue. Next adding action will dequeue the head.

The length of the queue can be changed at any time by changeMaxSize() method. But the new maxsize cannot be less than the current size of the queue.

Example Code

>>>
>>> from pcds import queue as q
>>> q1=q.Queue(5)
>>> q1.getMaxSize()
5
>>> q1
<Queue [0/5], Head:():Tail>
>>>
>>> q1.enQueue(15)
>>> q1.enQueue(1.85)
>>> q1.enQueue('hello')
>>> q1.enQueue(True)
>>> q1.enQueue(False)
>>> q1.enQueue(None)
Queue is already full. So, head is dequeued.
15
>>> q1.enQueue([1,2,'a','b'])
Queue is already full. So, head is dequeued.
1.85
>>> q1
<Queue [5/5], Head:('hello', True, False, None, [1, 2, 'a', 'b']):Tail>
>>>
>>> q1.enQueue({'name':'ali','age':22})
Queue is already full. So, head is dequeued.
'hello'
>>>
>>> q1
<Queue [5/5], Head:(True, False, None, [1, 2, 'a', 'b'], {'name': 'ali', 'age': 22}):Tail>
>>>
>>> q1.deQueue()
True
>>> q1.deQueue()
False
>>> q1.deQueue()
>>> q1.deQueue()
[1, 2, 'a', 'b']
>>> q1.deQueue()
{'name': 'ali', 'age': 22}
>>> q1.deQueue()
Queue is empty.
>>> q1
<Queue [0/5], Head:():Tail>
>>>

Now, if data is loaded from the data file, the maxsize of the queue will be updated if the current maxsize is less than the loaded data size.

Example Code

>>>
>>> q1.clear()
>>> q1
<Queue [0/5], Head:():Tail>
>>> q1.getMaxSize()
5
>>>
>>> q1.loadDataFromFile()
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\queue.pcds'
True
>>> q1.getMaxSize()
20
>>> q1
<Queue [11/20], Head:(10, 30, 40.87, 'hello', True, False, None, 0, [1, 3, 5, 'a', 'b', 'c', 47], (1, 5, 8, 'world', [4, 7, 8, {'name': 'ali'}]), 'world'):Tail>
>>>
>>> q1.changeMaxSize(10)
Warning! The length of the queue cannot be less than the current size.
>>> q1.getMaxSize()
20
>>>

copy() method copies the queue structure to a new instance

>>>
>>> from pcds import queue as q
>>> q1=q.Queue()
>>> q1
<Queue [0/100], Head:():Tail>
>>>
>>> q1.changeMaxSize(5)
>>> q1
<Queue [0/5], Head:():Tail>
>>>
>>> q1.enQueue(20)
>>> q1
<Queue [1/5], Head:(20):Tail>
>>>
>>> q2=q1.copy()
>>> q2
<Queue [1/5], Head:(20):Tail>
>>> q2.enQueue(25.23)
>>> q2.enQueue('abc')
>>> q2.enQueue(True)
>>> q2.enQueue('hello')
>>> q2
<Queue [5/5], Head:(20, 25.23, 'abc', True, 'hello'):Tail>
>>>
>>> q2.enQueue(100)
Queue is already full. So, head is dequeued.
20
>>> q2
<Queue [5/5], Head:(25.23, 'abc', True, 'hello', 100):Tail>
>>> q2.enQueue('ebook')
Queue is already full. So, head is dequeued.
25.23
>>> q2
<Queue [5/5], Head:('abc', True, 'hello', 100, 'ebook'):Tail>
>>>
>>> q1
<Queue [1/5], Head:(20):Tail>
>>>

Priority Queue Data Structure

Priority Queue is a FIFO (First In First Out) type data structure where data have priorities. Priority values are integers ranging from 0 to 100 where greater the value, higher the priority. Data with the greater priority is dequeued first. Data is entered by enQueue() method and is taken out by deQueue() method. The oldest data with the highest priority in the queue is called the head. When the data are accessed, the head will be dequeued first. Queue has a resetable finite length. Default size is 100.

The size of the queue is to be set when the queue instance is created. When the data are entered in the queue, if queue is full, the head will be dequeued automatically.

Queue data can be saved by save() method at any time either in encrypted or in plain text form.

Default data file address: home+"\\pcds_data\\pqueue.pcds"

Example Code

>>>
>>> from pcds import pqueue as pq
>>> q1=pq.PQueue()
>>>
>>> q1.enQueue(10,20)
>>> q1.enQueue(13.48,15)
>>> q1.enQueue('hello',22)
>>> q1.enQueue('abc',12)
>>> q1.enQueue(True,30)
>>> q1.enQueue(False,10)
>>> q1.enQueue(None,0)
>>>
>>> q1.enQueue([1,2,3],32)
>>> q1.enQueue(['a',(4,5,{'name':'ali','age':15}),2,3],32)
>>>
>>> q1
<PriorityQueue [9/100], Head:({'data': [1, 2, 3], 'priority': 32}, {'data': ['a', (4, 5, {'name': 'ali', 'age': 15}), 2, 3], 'priority': 32}, {'data': True, 'priority': 30}, {'data': 'hello', 'priority': 22}, {'data': 10, 'priority': 20}, {'data': 13.48, 'priority': 15}, {'data': 'abc', 'priority': 12}, {'data': False, 'priority': 10}, {'data': None, 'priority': 0}):Tail>
>>>
>>>
>>> q1.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\pqueue.pcds'
True
>>>
>>> q1.clear()
>>> q1
<PriorityQueue [0/100], Head:():Tail>
>>>
>>> q2=q1.copy()
>>>
>>> q2.loadDataFromFile()
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\pqueue.pcds'
True
>>>
>>> q2
<PriorityQueue [9/100], Head:('data":[1,2,3],"priority":32', {'data': ['a', (4, 5, {'name': 'ali', 'age': 15}), 2, 3], 'priority': 32}, {'data': True, 'priority': 30}, {'data': 'hello', 'priority': 22}, {'data': 10, 'priority': 20}, {'data': 13.48, 'priority': 15}, {'data': 'abc', 'priority': 12}, {'data': False, 'priority': 10}, {'data': None, 'priority': 0}):Tail>
>>>
>>> q1
<PriorityQueue [0/100], Head:():Tail>
>>>

Creating a small queue of length 5

Queue will be full if there are five data in the queue. Next adding action will dequeue the head.

The length of the queue can be changed at any time by changeMaxSize() method. But the new maxsize cannot be less than the current size of the queue.

Example Code

>>>
>>> from pcds import pqueue as pq
>>> q1=pq.PQueue(5)
>>> q1.enQueue(10,20) # priority=20
>>> q1.enQueue(45.89,30) # priority=20
>>> q1.enQueue('hello',15) # priority=15
>>> q1.enQueue('world',25) # priority=25
>>> q1.enQueue(True,35) # priority=35
>>> q1.enQueue('world',25) # priority=25
Priority queue is full. So, head is dequeued.
True
>>> q1.enQueue(False,50) # priority=50
Priority queue is full. So, head is dequeued.
False
>>> q1.enQueue('ali',5) # priority=5
Priority queue is full. So, head is dequeued.
45.89
>>> q1.enQueue('minu',40) # priority=40
Priority queue is full. So, head is dequeued.
'minu'
>>>
>>>

Now, if data is loaded from the data file, the maxsize of the queue will be updated if the current maxsize is less than the loaded data size.

Example Code

>>>
>>> q1.clear()
>>> q1
<PriorityQueue [0/5], Head:():Tail>
>>> q1.loadDataFromFile()
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\pqueue.pcds'
True
>>>
>>> len(q1)
9
>>> q1.getMaxSize()
10
>>> q1
<PriorityQueue [9/10], Head:('data":[1,2,3],"priority":32', {'data': ['a', (4, 5, {'name': 'ali', 'age': 15}), 2, 3], 'priority': 32}, {'data': True, 'priority': 30}, {'data': 'hello', 'priority': 22}, {'data': 10, 'priority': 20}, {'data': 13.48, 'priority': 15}, {'data': 'abc', 'priority': 12}, {'data': False, 'priority': 10}, {'data': None, 'priority': 0}):Tail>
>>>
>>>

LinkedList Data Structure

Singly linked list is made of nodes having next pointer only. So, list cursor can travel in forward direction only. Singly linked list has a name and its instance has to be created by giving a name. The default name is LNKList. Nodes have index values starting from zero. seek() and tell() methods are used to move the cursor along the list to any node.

Supported Data Types: int, float, string, bool, NoneType, list, tuple, dictionary

Example Code

>>>
>>> from ntnds import linkedlist as lnkl
>>> ll=lnkl.LinkedList()
>>> ll.append(10)
>>> ll.append(20)
>>> ll.append(30.48)
>>> ll.append('hello')
>>> ll.append(True)
>>> ll.append([1,2,'hello',True,None,False])
>>> ll.append((78,45.12,{'a':45,'b':17},'abc'))
>>> ll.append(False)
>>> ll.append(None)
Warning! None type data is entered.
>>>
>>> ll
<LNKList[LinkedList]: size=9, Data=(10, 20, 30.48, 'hello', True, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), False, None)>
>>>
>>> ll.convert2dict()
{'name': 'LNKList', 'class': 'LinkedList', 'size': 9, 0: 10, 1: 20, 2: 30.48, 3: 'hello', 4: True, 5: [1, 2, 'hello', True, None, False], 6: (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 7: False, 8: None}
>>>

Adding data from a list or tuple

>>>
>>> ll.clear()
>>> ll
<LNKList[LinkedList]: size=0, Data=()>
>>> datalist=[10,20,45.12,True,False,'hello',{1:50,2:100,'a':'abc'}]
>>> ll.addDataFromList(datalist)
>>> ll
<LNKList[LinkedList]: size=7, Data=(10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'})>
>>>
>>> datatuple=(8,1,7.8,['ab','bc','cd'],{'name':'ali','age':12})
>>> ll.addDataFromTuple(datatuple)
>>> ll
<LNKList[LinkedList]: size=12, Data=(10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>

Moving in the list using moveFirst(), moveNext(), moveLast(), getNodeAt() methods

>>>
>>> ll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 11>
>>> ll.moveFirst()
>>> ll.getCurrentNode()
<Node: 10 at index 0>
>>> ll.getNodeAt(7)
<Node: 8 at index 7>
>>> ll.getCurrentNode()
<Node: 8 at index 7>
>>> ll.moveNext()
>>> ll.getCurrentNode()
<Node: 1 at index 8>
>>> ll.moveNext()
>>> ll.getCurrentNode()
<Node: 7.8 at index 9>
>>> ll.moveLast()
>>> ll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 11>
>>> ll.getNodeAt(4)
<Node: False at index 4>
>>> ll.getCurrentNode()
<Node: False at index 4>
>>>

Moving in the list using tell() and seek() methods

>>>
>>> ll.convert2dict()
{'name': 'LNKList', 'class': 'LinkedList', 'size': 12, 0: 10, 1: 20, 2: 45.12, 3: True, 4: False, 5: 'hello', 6: {1: 50, 2: 100, 'a': 'abc'}, 7: 8, 8: 1, 9: 7.8, 10: ['ab', 'bc', 'cd'], 11: {'name': 'ali', 'age': 12}}
>>>
>>> ll.tell()
4
>>> ll.getCurrentNode()
<Node: False at index 4>
>>>
>>> ll.seek(0,0)
>>> ll.getCurrentNode()
<Node: 10 at index 0>
>>> ll.seek(1,0)
>>> ll.getCurrentNode()
<Node: 20 at index 1>
>>> ll.seek(10,0)
>>> ll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 10>
>>> ll.seek(5,0)
>>> ll.getCurrentNode()
<Node: hello at index 5>
>>>
>>> ll.seek(0,1)
>>> ll.getCurrentNode()
<Node: hello at index 5>
>>>
>>> ll.seek(-1,1)
>>> ll.getCurrentNode()
<Node: False at index 4>
>>> ll.seek(1,1)
>>> ll.getCurrentNode()
<Node: hello at index 5>
>>> ll.seek(1,1)
>>> ll.getCurrentNode()
<Node: {1: 50, 2: 100, 'a': 'abc'} at index 6>
>>>
>>> ll.seek(0,2)
>>> ll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 11>
>>> ll.seek(-1,2)
>>> ll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 10>
>>>
>>> ll.seek(2,2)
Index out of range.
>>> ll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 10>
>>>
>>> ll.seek(-2,0)
Index out of range.
>>> ll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 10>
>>>
>>> ll.seek(-1,0)
Index out of range.
>>> ll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 10>
>>>

saving data into the disk and loading from the disk

List data can be saved by save(directory, filename, preserve, enc) method. Directory and filename (without extension) describes the data file; preserve is a boolean parameter which states whether a datafile can be deleted or not; and enc is another boolean parameter which states whether encrypted or plain data are to be saved. Data can be loaded from the data file by loadDataFromFile(fileaddress) method.

Default directory = home + "\\pcds_data"
Default filename = "linkedlist.pcds"
Default preserve value = True
Default enc value = True

>>>
>>> ll
<LNKList[LinkedList]: size=15, Data=('First', 'second', 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'xyz')>
>>>
>>> ll.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\linkedlist.pcds'
True
>>>
>>> ll.clear()
>>> ll
<LNKList[LinkedList]: size=0, Data=()>
>>> ll.loadDataFromFile()
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\linkedlist.pcds'
True
>>> ll
<LNKList[LinkedList]: size=15, Data=('First', 'second', 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'xyz')>
>>>

insert, edit and delete data

New data can be inserted at an index position by insertDataAt(data, index) method. A data of a certain index can be deleted by deleteDataAt(index) method, and a data can be edited by editDataAt(data, index) method.

>>>
>>> ll
<LNKList[LinkedList]: size=12, Data=(10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>
>>> ll.insertDataAt('First')
>>> ll
<LNKList[LinkedList]: size=13, Data=('First', 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>
>>> ll.insertDataAt('abc',-1)
Index out of the range.
>>> ll.insertDataAt('abc',14)
Index out of the range.
>>> ll.insertDataAt('abc',13)
>>> ll
<LNKList[LinkedList]: size=14, Data=('First', 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'abc')>
>>>
>>> ll.insertDataAt('Bangladesh',6)
>>> ll
<LNKList[LinkedList]: size=15, Data=('First', 10, 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'abc')>
>>>
>>>
>>> ll.editDataAt('xyz',14)
>>> ll
<LNKList[LinkedList]: size=15, Data=('First', 10, 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'xyz')>
>>>
>>> ll.insertDataAt('second',1)
>>> ll
<LNKList[LinkedList]: size=16, Data=('First', 'second', 10, 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'xyz')>
>>>
>>> ll.deleteDataAt(1)
>>> ll
<LNKList[LinkedList]: size=15, Data=('First', 10, 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'xyz')>
>>>
>>> ll.editDataAt('second',1)
>>> ll
<LNKList[LinkedList]: size=15, Data=('First', 'second', 20, 45.12, True, False, 'Bangladesh', 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'xyz')>
>>>

copy() method copies the linked list structure to a new instance

>>>
>>> from pcds import linkedlist as lnkl
>>> ll=lnkl.LinkedList('mylist1')
>>> ll
<mylist1[LinkedList]: size=0, Data=()>
>>> ll.append(45)
>>> ll2=ll.copy()
>>> ll2
<mylist1[LinkedList]: size=1, Data=(45,)>
>>> ll2.addDataFromList([48.12,'hello',True,False,None,'abc'])
Warning! None type data is entered.
>>> ll2
<mylist1[LinkedList]: size=7, Data=(45, 48.12, 'hello', True, False, None, 'abc')>
>>> ll2.tell()
6
>>> ll2.seek(0,0)
>>> ll2.getCurrentNode()
<Node: 45 at index 0>
>>> ll2.moveNext()
>>> ll2.getCurrentNode()
<Node: 48.12 at index 1>
>>> ll2.moveLast()
>>> ll2.getCurrentNode()
<Node: 'abc' at index 6>
>>>

DLinkedList (Doubly Linked List) Data Structure

DLinkedList (doubly linked list) has the same operations, the singly linked list has, but it is more efficient in moving around in the list since it has next and prev pointers by which the cursor can move both in forward and backard directions.

DLinkedList instance has to be created using a name. Data can be added, inserted, edited and deleted. Data can be saved into the hard disk.

Supported Data Types: int, float, string, bool, NoneType, list, tuple, dictionary

Example Code

>>>
>>> from ntnds import dlinkedlist as dlnkl
>>> dll=dlnkl.DLinkedList()
>>> dll.append(10)
>>> dll.append(20)
>>> dll.append(30.48)
>>> dll.append([1,2,'hello',True,None,False])
>>> dll.append((78,45.12,{'a':45,'b':17},'abc'))
>>> dll.append('yes')
>>> dll.append(True)
>>> dll.append(False)
>>> dll.append(None)
Warning! None type data is entered.
>>>
>>> dll
<DLNKList[DLinkedList]: size=9, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None)>
>>>
>>> dll.convert2dict()
{'name': 'DLNKList', 'class': 'DLinkedList', 'size': 9, 0: 10, 1: 20, 2: 30.48, 3: [1, 2, 'hello', True, None, False], 4: (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 5: 'yes', 6: True, 7: False, 8: None}
>>>

Adding data from a list or tuple

>>>
>>> datalist=[10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}]
>>> dll.addDataFromList(datalist)
>>> dll
<DLNKList[DLinkedList]: size=16, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'})>
>>>
>>> datatuple=(8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})
>>>
>>> dll.addDataFromTuple(datatuple)
>>> dll
<DLNKList[DLinkedList]: size=21, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>

saving data into the disk and loading data from the disk

List data can be saved by save(directory, filename, preserve, enc) method. Directory and filename (without extension) describes the data file; preserve is a boolean parameter which describes whether a datafile can be deleted; and enc is another boolean parameter which states whether encrypted or plain data are to be saved. Data can be loaded from the data file by loadDataFromFile(fileaddress) method.

Default directory: home + "\\pcds_data"
Default filename = "dlinkedlist"
Default preserve value = True
Default enc value = True

>>>
>>> dll.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\dlinkedlist.pcds'
True
>>>
>>> dll.clear()
>>> dll
<DLNKList[DLinkedList]: size=0, Data=()>
>>> dll.loadDataFromFile()
Warning! None type data is entered.
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\dlinkedlist.pcds'
True
>>>
>>> dll
<DLNKList[DLinkedList]: size=21, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>

Moving in the list using moveFirst(), moveNext(), moveLast(), getNodeAt() methods

>>>
>>> dll.getCurrentNode()
<Head Node at index -1>
>>> dll.moveNext()
>>> dll.getCurrentNode()
<Node: 10 at index 0>
>>> dll.moveNext()
>>> dll.getCurrentNode()
<Node: 20 at index 1>
>>> dll.moveLast()
>>> dll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> dll.moveNext()
>>> dll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> dll.movePrev()
>>> dll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 19>
>>> dll.movePrev()
>>> dll.getCurrentNode()
<Node: 7.8 at index 18>
>>> dll.moveFirst()
>>> dll.getCurrentNode()
<Node: 10 at index 0>
>>> dll.movePrev()
>>> dll.getCurrentNode()
<Node: 10 at index 0>
>>> dll.getNodeAt(10)
<Node: 20 at index 10>
>>> dll.movePrev()
>>> dll.getCurrentNode()
<Node: 10 at index 9>
>>> dll.getNodeAt(10)
<Node: 20 at index 10>
>>> dll.moveNext()
>>> dll.getCurrentNode()
<Node: 45.12 at index 11>
>>>

Moving to head by moveToHead() method

>>>
>>> dll.getCurrentNode()
<Node: 45.12 at index 11>
>>>
>>> dll.moveToHead()
>>> dll.getCurrentNode()
<Head Node at index -1>
>>>

Moving in the list using tell() and seek() methods

>>>
>>> dll.tell()
-1
>>> dll.seek(0,0)
>>> dll.getCurrentNode()
<Node: 10 at index 0>
>>> dll.tell()
0
>>> dll.seek(10,0)
>>> dll.getCurrentNode()
<Node: 20 at index 10>
>>>
>>> dll.seek(0,2)
>>> dll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> dll.seek(2,2)
>>> dll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> dll.seek(-2,2)
>>> dll.getCurrentNode()
<Node: 7.8 at index 18>
>>>
>>> dll.seek(-10,2)
>>> dll.tell()
10
>>> dll.seek(2,1)
>>> dll.getCurrentNode()
<Node: True at index 12>
>>> dll.seek(-3,1)
>>> dll.getCurrentNode()
<Node: 10 at index 9>
>>>

insert, edit and delete data

New data can be inserted at an index position by insertDataAt(data, index) method. A data of a certain index can be deleted by deleteDataAt(index) method, and a data can be edited by editDataAt(data, index) method.

>>>
>>>
>>> dll
<DLNKList[DLinkedList]: size=21, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>
>>> dll.insertDataAt('world',3)
>>>
>>> dll
<DLNKList[DLinkedList]: size=22, Data=(10, 20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>
>>> dll.insertDataAt('END',22)
>>> dll
<DLNKList[DLinkedList]: size=23, Data=(10, 20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>
>>> dll.editDataAt(10.2,0)
>>> dll
<DLNKList[DLinkedList]: size=23, Data=(10.2, 20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>
>>> dll.deleteDataAt(0)
>>> dll
<DLNKList[DLinkedList]: size=22, Data=(20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>
>>> dll.deleteDataAt(5)
>>> dll
<DLNKList[DLinkedList]: size=21, Data=(20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>

copy() method copies the linked list structure to a new instance

>>>
>>> from pcds import dlinkedlist as dlnkl
>>> dll=dlnkl.DLinkedList('dll')
>>> dll
<dll[DLinkedList]: size=0, Data=()>
>>> dll.append(56)
>>> dll.append('hello')
>>> dll
<dll[DLinkedList]: size=2, Data=(56, 'hello')>
>>> 
>>> dll2=dll.copy()
>>> dll2
<dll[DLinkedList]: size=2, Data=(56, 'hello')>
>>> dll2.append(True)
>>> dll2.addDataFromTuple(('ebook',41.2,100))
>>> dll2
<dll[DLinkedList]: size=6, Data=(56, 'hello', True, 'ebook', 41.2, 100)>
>>> dll2.seek(0,0)
>>> dll2.getCurrentNode()
<Node: 56 at index 0>
>>> dll2.seek(0,2)
>>> dll2.getCurrentNode()
<Node: 100 at index 5>
>>>

CLinkedList (Circular Linked List) Data Structure

CLinkedList (circular linked list) is a doubly linked list where the first and the last nodes are joined by the forward and backward links. The data structure can be rotated both in forward and backard directions. All other operations are identical to the operations of the dynamic linked list.

CLinkedList instance has to be created using a name. Data can be added, inserted, edited and deleted. Data can be saved into the hard disk.

Supported Data Types: int, float, string, bool, NoneType, list, tuple, dictionary

Example Code

>>>
>>> from pcds import clinkedlist as clnkl
>>> cll=clnkl.CLinkedList('myclinkedlist')
>>> cll.append(10)
>>> cll.append(20)
>>> cll.append(30.48)
>>> cll.append([1,2,'hello',True,None,False])
>>> cll.append((78,45.12,{'a':45,'b':17},'abc'))
>>> cll.append('yes')
>>> cll.append(True)
>>> cll.append(False)
>>> cll.append(None)
Warning! None type data is entered.
>>>
>>> cll
<myclinkedlist[CLinkedList]: size=9, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None)>
>>>
>>> cll.convert2dict()
{'name': 'myclinkedlist', 'class': 'CLinkedList', 'size': 9, 0: 10, 1: 20, 2: 30.48, 3: [1, 2, 'hello', True, None, False], 4: (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 5: 'yes', 6: True, 7: False, 8: None}
>>>

Adding data from a list or tuple

>>>
>>> datalist=[10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}]
>>> cll.addDataFromList(datalist)
>>> cll
<myclinkedlist[CLinkedList]: size=16, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'})>
>>>
>>> datatuple=(8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})
>>>
>>> cll.addDataFromTuple(datatuple)
>>> cll
<myclinkedlist[CLinkedList]: size=21, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>

saving data into the disk and loading data from the disk

List data can be saved by save(directory, filename, preserve, enc) method. Directory and filename (without extension) describes the data file; preserve is a boolean parameter which describes whether a datafile can be deleted; and enc is another boolean parameter which states whether encrypted or plain data are to be saved. Data can be loaded from the data file by loadDataFromFile(fileaddress) method.

Default directory: home + "\\pcds_data"
Default filename = "clinkedlist"
Default preserve value = True
Default enc value = True

>>>
>>> cll.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\myclinkedlist.pcds'
True
>>>
>>> cll.clear()
>>> cll
<myclinkedlist[CLinkedList]: size=0, Data=()>
>>> cll.loadDataFromFile()
Warning! None type data is entered.
Data Loaded Successfully from the file: 'C:\Users\admin\pcds_data\myclinkedlist.pcds'
True
>>>
>>> cll
<myclinkedlist[CLinkedList]: size=21, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>

Moving in the list using moveFirst(), moveNext(), moveLast(), getNodeAt() methods

>>>
>>> cll.getCurrentNode()
<Head Node at index -1>
>>> cll.moveNext()
>>> cll.getCurrentNode()
<Node: 10 at index 0>
>>> cll.moveNext()
>>> cll.getCurrentNode()
<Node: 20 at index 1>
>>> cll.moveLast()
>>> cll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> cll.moveNext()
>>> cll.getCurrentNode()
<Node: 10 at index 0>
>>> cll.movePrev()
>>> cll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> cll.movePrev()
>>> cll.getCurrentNode()
<Node: ['ab', 'bc', 'cd'] at index 19>
>>> cll.moveFirst()
>>> cll.getCurrentNode()
<Node: 10 at index 0>
>>> cll.movePrev()
>>> cll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> cll.getNodeAt(10)
<Node: 20 at index 10>
>>> cll.movePrev()
>>> cll.getCurrentNode()
<Node: 10 at index 9>
>>> cll.getNodeAt(10)
<Node: 20 at index 10>
>>> cll.moveNext()
>>> cll.getCurrentNode()
<Node: 45.12 at index 11>
>>>
>>>

Moving to head by moveToHead() method

>>>
>>> cll.getCurrentNode()
<Node: 45.12 at index 11>
>>>
>>> cll.moveToHead()
>>> cll.getCurrentNode()
<Head Node at index -1>
>>>

Moving in the list using tell() and seek() methods

>>>
>>> cll.tell()
-1
>>> cll.seek(0,0)
>>> cll.getCurrentNode()
<Node: 10 at index 0>
>>> cll.tell()
0
>>> cll.seek(10,0)
>>> cll.getCurrentNode()
<Node: 20 at index 10>
>>>
>>> cll.seek(0,2)
>>> cll.getCurrentNode()
<Node: {'name': 'ali', 'age': 12} at index 20>
>>> cll.seek(2,2)
>>> cll.getCurrentNode()
<Node: 20 at index 1>
>>> cll.seek(-2,2)
>>> cll.getCurrentNode()
<Node: 7.8 at index 18>
>>> cll.seek(-10,2)
>>> cll.tell()
10
>>> cll.seek(2,1)
>>> cll.getCurrentNode()
<Node: True at index 12>
>>> cll.seek(-3,1)
>>> cll.getCurrentNode()
<Node: 10 at index 9>
>>>

insert, edit and delete data

New data can be inserted at an index position by insertDataAt(data, index) method. A data of a certain index can be deleted by deleteDataAt(index) method, and a data can be edited by editDataAt(data, index) method.

>>>
>>>
>>> cll
<myclinkedlist[CLinkedList]: size=21, Data=(10, 20, 30.48, [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>
>>> cll.insertDataAt('world',3)
>>> cll
<myclinkedlist[CLinkedList]: size=22, Data=(10, 20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12})>
>>>
>>> cll.insertDataAt('END',22)
>>> cll
<myclinkedlist[CLinkedList]: size=23, Data=(10, 20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>
>>> cll.editDataAt(10.2,0)
>>> cll
<myclinkedlist[CLinkedList]: size=23, Data=(10.2, 20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>
>>> cll.deleteDataAt(0)
>>> cll
<myclinkedlist[CLinkedList]: size=22, Data=(20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), 'yes', True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>
>>> cll.deleteDataAt(5)
>>> cll
<myclinkedlist[CLinkedList]: size=21, Data=(20, 30.48, 'world', [1, 2, 'hello', True, None, False], (78, 45.12, {'a': 45, 'b': 17}, 'abc'), True, False, None, 10, 20, 45.12, True, False, 'hello', {1: 50, 2: 100, 'a': 'abc'}, 8, 1, 7.8, ['ab', 'bc', 'cd'], {'name': 'ali', 'age': 12}, 'END')>
>>>

copy(), rotateRight() and rotateLeft() methods

>>> from pcds import clinkedlist as clnkl
>>>
>>> cll=clnkl.CLinkedList('myclinkedlist')
>>>
>>> cll
<myclinkedlist[CLinkedList]: size=0, Data=()>
>>>
>>> cll2=cll.copy()
>>> cll2
<myclinkedlist[CLinkedList]: size=0, Data=()>
>>>
>>> cll2.append(10)
>>> cll2.append(12.45)
>>> cll2.append('hello')
>>> cll2.append(True)
>>> cll2.append(False)
>>> cll2.append(None)
Warning! None type data is entered.
>>>
>>> cll2.append([1,2,3])
>>> cll2.append((4,5))
>>> cll2.append({1:'book',2:'pencil'})
>>>
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=(10, 12.45, 'hello', True, False, None, [1, 2, 3], (4, 5), {1: 'book', 2: 'pencil'})>
>>>
>>> cll
<myclinkedlist[CLinkedList]: size=0, Data=()>
>>>
>>> cll2.rotateRight()
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=({1: 'book', 2: 'pencil'}, 10, 12.45, 'hello', True, False, None, [1, 2, 3], (4, 5))>
>>> cll2.rotateRight()
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=((4, 5), {1: 'book', 2: 'pencil'}, 10, 12.45, 'hello', True, False, None, [1, 2, 3])>
>>> cll2.rotateRight(10)
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=([1, 2, 3], (4, 5), {1: 'book', 2: 'pencil'}, 10, 12.45, 'hello', True, False, None)>
>>>
>>> cll2.rotateLeft()
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=((4, 5), {1: 'book', 2: 'pencil'}, 10, 12.45, 'hello', True, False, None, [1, 2, 3])>
>>> cll2.rotateLeft()
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=({1: 'book', 2: 'pencil'}, 10, 12.45, 'hello', True, False, None, [1, 2, 3], (4, 5))>
>>> cll2.rotateLeft()
>>> cll2
<myclinkedlist[CLinkedList]: size=9, Data=(10, 12.45, 'hello', True, False, None, [1, 2, 3], (4, 5), {1: 'book', 2: 'pencil'})>
>>>

BSTree (Binary Search Tree) Data Structure

BSTree is a binary tree made of nodes which carry data and which links with other nodes forming a tree structure. Each node has three links: parent, left and right. The left, right links are used to join the left and right child nodes respectively and the parent link to the parent node.

Order of the data values of the nodes: parent node > right node > node > left node.

BSTree is very efficient in sorting and searching operations. Currently, only the string and numeric data types are supported. Tree data can be saved in the disk and data can also be loaded from the disk.

Example Code

>>>
>>> from ntnds import bstree as bst
>>> t1=bst.BinarySearchTree()
>>> t1
()
>>> t1.append(10)
>>> t1.append(20)
>>> t1.append('hi')
>>> t1.loadDataFromList([2,7,4,8,0,5.6,9.8,4.7,8.1])
>>> t1
(10, 20, 'hi', 2, 7, 4, 8, 0, 5.6, 9.8, 4.7, 8.1)
>>> str(t1)
"<Binary Search Tree(not sorted): no. of nodes=12, data=(10, 20, 'hi', 2, 7, 4, 8, 0, 5.6, 9.8, 4.7, 8.1)>"
>>>
>>> t1.loadDataFromTuple(('m','x','a','y','f','b','t','p','a','b'))
>>> t1.loadDataFromList([1,2,3,4,5,6,7,8,9])
>>>
>>> t1
(10, 20, 'hi', 2, 7, 4, 8, 0, 5.6, 9.8, 4.7, 8.1, 'm', 'x', 'a', 'y', 'f', 'b', 't', 'p', 'a', 'b', 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>>
>>> len(t1)
31
>>>
>>> t1.buildTree()
>>> t1.sortTree()
>>>
>>> t1
(0, 1, 2, 3, 4, 4.7, 5, 5.6, 6, 7, 8, 8.1, 9, 9.8, 10, 20, 'a', 'b', 'f', 'm', 'p', 't', 'x', 'y', 'hi')
>>>
>>> len(t1)
25
>>>
>>> t1.sortTree(asc=False)
>>> t1
('hi', 'y', 'x', 't', 'p', 'm', 'f', 'b', 'a', 20, 10, 9.8, 9, 8.1, 8, 7, 6, 5.6, 5, 4.7, 4, 3, 2, 1, 0)
>>>
>>>
>>> t1.save()
Encrypted Data Saved Successfully into 'C:\Users\admin\pcds_data\bstree.pcds'
True
>>>
>>> t1.clear()
>>> t1
()
>>> t1.loadDataFromFile()
Data Loaded Successfully.
True
>>>
>>> t1
(0, 1, 2, 3, 4, 4.7, 5, 5.6, 6, 7, 8, 8.1, 9, 9.8, 10, 20, 'a', 'b', 'f', 'm', 'p', 't', 'x', 'y', 'hi')
>>>

Finding the minimum and maximum data by min() and max() functions

BSTree module has min() and max() functions which can find the minimum and maximum data of a data list. Only numeric and string data are supported. String data are converted to integer first before calculation.

>>>
>>> from ntnds import bstree as bst
>>> datalist=[4.89,4.05,45,12,74,85.26,95.21,77,1.24,0.85,'a','m','x',80,'ab','xy',245,614]
>>> bst.min(datalist)
0.85
>>> bst.max(datalist)
'xy'
>>>

Sorting data by sort() function

BSTree module has sort() function which can sort data in a list efficiently either in ascending or in descending order. If asc=True data are sorted in ascending order, and if asc=False, data are sorted in descending order.

>>>
>>> from ntnds import bstree as bst
>>> datalist=[4.89,4.05,45,12,74,85.26,95.21,77,1.24,0.85,'a','m','x',80,'ab','xy',245,614]
>>>
>>> bst.sort(datalist)
(0.85, 1.24, 4.05, 4.89, 12, 45, 'a', 74, 77, 80, 85.26, 'x', 95.21, 245, 614, 'ab', 'xy')
>>>
>>> bst.sort(datalist,asc=False)
('xy', 'ab', 614, 245, 95.21, 'x', 85.26, 80, 77, 74, 'a', 45, 12, 4.89, 4.05, 1.24, 0.85)
>>>
>>> bst.sort(datalist,asc=True)
(0.85, 1.24, 4.05, 4.89, 12, 45, 'a', 74, 77, 80, 85.26, 'x', 95.21, 245, 614, 'ab', 'xy')
>>>

Searching data by search() function

BSTree module has search() function that returns the matching index of the sorted data list. If data is not found, None is returned.

>>>
>>> from ntnds import bstree as bst
>>> datalist=[4.89,4.05,45,12,74,85.26,95.21,77,1.24,0.85,'a','m','x',80,'ab','xy',245,614]
>>>
>>> bst.search(50,datalist)
>>> bst.search(45,datalist)
5
>>> bst.search('ab',datalist)
15
>>> bst.search(4,datalist)
>>> bst.search(4.05,datalist)
2
>>>

Set Data Structure

This python module creates a set data structure which has unique data with the following operations: union (AUB or A+B), intersection (A.B), difference (A-B), and complement (A`= Universal-A) operations.

Suported Data Types: int, float, str, bool (False=0, True=1)

Data are saved in a binary file with extension .pcds (python common data structure). Data encryption is supported for security.

Example Code

>>>
>>> from pcds import set
>>> s1=set.Set(['ebook','blue book','chemistry book','math book'])
>>> s1
<Set: size=4, data=('ebook', 'blue book', 'math book', 'chemistry book')>
>>> len(s1)
4
>>> s1.addDataFromTuple(('workbook','exercise book','lab book'))
>>> s1
<Set: size=7, data=('ebook', 'lab book', 'workbook', 'blue book', 'math book', 'exercise book', 'chemistry book')>
>>>
>>> s1.addDataFromList(['workbook','exercise book','lab book'])
>>> s1
<Set: size=7, data=('ebook', 'lab book', 'workbook', 'blue book', 'math book', 'exercise book', 'chemistry book')>
>>>
>>> s2=s1.copy()
>>> s2
<Set: size=7, data=('ebook', 'lab book', 'workbook', 'blue book', 'math book', 'exercise book', 'chemistry book')>
>>>

Set operations

Set is a mathematical structure having the following operations: union (+), intersection (^) and subtraction (-). Subtraction operation is also known as difference operation. Complement operation is nothing but the subtraction of a set from the universal set.

>>>
>>> from pcds import set
>>> set1=set.Set([45,12,14,12.65,2.17,30,0,-6,-20,40,15,14,0,10,20,50,14, 12,45,60,42,47,55,58])
>>> set1
<Set: size=19, data=(-20, -6, 0, 2.17, 10, 12, 12.65, 14, 15, 20, 30, 40, 42, 45, 47, 50, 55, 58, 60)>
>>> 
>>> set2=set.Set([30,10,40,50,60,0,20])
>>> set2
<Set: size=7, data=(0, 10, 20, 30, 40, 50, 60)>
>>> set3=set.Set([10,50,20,30,40,60,44,23,27,28,33,45,48])
>>> set3
<Set: size=13, data=(10, 20, 23, 27, 28, 30, 33, 40, 44, 45, 48, 50, 60)>
>>>
>>> set4=set.Set()
>>> set4.isNull()
True
>>>
>>> set4.addDataFromTuple((56,4,36,46,6,16,26,))
>>> set4
<Set: size=7, data=(4, 6, 16, 26, 36, 46, 56)>
>>>
>>> set1 + set2
<Set: size=19, data=(-20, -6, 0, 2.17, 10, 12, 12.65, 14, 15, 20, 30, 40, 42, 45, 47, 50, 55, 58, 60)>
>>>
>>> set2 + set3
<Set: size=14, data=(0, 10, 20, 23, 27, 28, 30, 33, 40, 44, 45, 48, 50, 60)>
>>>
>>> set3 + set4
<Set: size=20, data=(4, 6, 10, 16, 20, 23, 26, 27, 28, 30, 33, 36, 40, 44, 45, 46, 48, 50, 56, 60)>
>>>
>>> set1 ^ set2
<Set: size=7, data=(0, 10, 20, 30, 40, 50, 60)>
>>> set2 ^ set3
<Set: size=6, data=(10, 20, 30, 40, 50, 60)>
>>> set3 ^ set4
<Set: size=0, data=()>
>>>
>>> set1 - set2
<Set: size=12, data=(-20, -6, 2.17, 12, 12.65, 14, 15, 42, 45, 47, 55, 58)>
>>> set2 - set1
<Set: size=0, data=()>
>>>
>>> set3 - set2
<Set: size=7, data=(23, 27, 28, 33, 44, 45, 48)>
>>> set2 - set3
<Set: size=1, data=(0,)>
>>>
>>> set1 - set4
<Set: size=19, data=(-20, -6, 0, 2.17, 10, 12, 12.65, 14, 15, 20, 30, 40, 42, 45, 47, 50, 55, 58, 60)>
>>> set4 - set1
<Set: size=7, data=(4, 6, 16, 26, 36, 46, 56)>
>>>
>>>

Boolean checks of the set structures

There are five boolean checks: equality check, null ckeck, disjoint check, superset check ans subset ckeck.

>>>
>>> set1=set.Set([45,12,14,12.65,2.17,30,0,-6,-20,40,15,14,0,10,20,50,14,12, 45,60,42,47,55,58])
>>> set1
<Set: size=19, data=(-20, -6, 0, 2.17, 10, 12, 12.65, 14, 15, 20, 30, 40, 42, 45, 47, 50, 55, 58, 60)>
>>>
>>> set2=set.Set([30,10,40,50,60,0,20])
>>> set2
<Set: size=7, data=(0, 10, 20, 30, 40, 50, 60)>
>>> set3=set.Set([10,50,20,30,40,60,44,23,27,28,33,45,48])
>>> set3
<Set: size=13, data=(10, 20, 23, 27, 28, 30, 33, 40, 44, 45, 48, 50, 60)>
>>>
>>> set4=set.Set()
>>> set4.isNull()
True
>>>
>>> set4.addDataFromTuple((56,4,36,46,6,16,26,))
>>> set4
<Set: size=7, data=(4, 6, 16, 26, 36, 46, 56)>
>>>
>>> set1 == set2
False
>>> set1 == set1
True
>>>
>>> set1 != set1
False
>>> set1 != set2
True
>>>
>>> set1.isDisjoint(set2)
False
>>> set1.isDisjoint(set3)
False
>>> set1.isDisjoint(set4)
True
>>> set2.isDisjoint(set3)
False
>>> set3.isDisjoint(set4)
True
>>>
>>> set1.isSuperset(set2)
True
>>> set1.isSuperset(set3)
False
>>> set1.isSuperset(set4)
False
>>>
>>> set4.isSubset(set1)
False
>>> set4.isSubset(set2)
False
>>> set4.isSubset(set3)
False
>>> set2.isSubset(set1)
True
>>> set3.isSubset(set1)
False
>>>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pcds-1.1.2-py3-none-any.whl (163.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page