Implementation of Python data structure

Posted by richo89 on Thu, 24 Feb 2022 12:04:23 +0100

preface

Data structure is the way that computer stores and organizes data. A data structure is a collection of data elements that have one or more specific relationships with each other. In general, carefully selected data structures can bring higher operation or storage efficiency. Data structure is often related to efficient retrieval algorithm and index technology.

I Parent class Struct

1. Why implement this class

1. It specifies the basic concept of data structure, which can standardize some basic functions and functions of various data
2. In order to look good (in fact, it's more normative)

2. Code

class Struct():
    def __len__(self):pass
    def __contains__(self,item):pass
    def Clear(self):pass
    def IsNull(self):pass

3. Interpretation

Naturally, needless to say, the len attribute is a data structure that stores data. Len can obtain the amount of data internally
Contents is implemented in the contents of the list. You can also write your own search algorithm
The Clear function is used to Clear
IsNull determines whether it is empty

II Stack stack

1. Definition of stack

Stack, also known as stack, is a linear table with limited operation. Limit linear tables to insert and delete only at the end of the table. This end is called the top of the stack, and the other end is called the bottom of the stack. Inserting a new element into a stack is also called stack entry, stack entry or stack pressing. It puts the new element above the top element of the stack to make it a new top element; Deleting an element from a stack is also called out of stack or out of stack. It deletes the top element of the stack and makes its adjacent elements become new top elements.

2. Code

import Struct
class Stack(Struct.Struct):
    StackSpace=1024
    def __init__(self):
        self.__index=-1 #Integer, pointer
        self.__datas=[None]*Stack.StackSpace #Data pool
    def __len__(self):
        if(self.__index<0):return 0
        else:return self.__index+1
    def __contains__(self,item):
        return self.__datas.__contains__(item)
    def IsNull(self):
        return self.__index<0      
    def Clear(self):
        self.__index=-1
        self.__datas.clear()

    def Push(self,obj):
        self.__index+=1
        self.__datas[self.__index]=obj
    def Pop(self):
        if(self.__index==-1):return False
        else:
            data=self.__datas[self.__index]
            del(self.__datas[self.__index])
        self.__index-=1
        return data
    def Top(self):
        if(self.__index==-1):return None
        else:return self.__datas[self.__index]

3. Interpretation

1. The push function is used to press the stack, and the list feature in Python determines that the stack is a multi-type structure. There is no limit on the data of pressing the stack, but the initial StackSpace specifies the maximum capacity of the stack. In fact, list is used Append to add elements is a better solution, but StackSpace also plays the role of data specification
2.Pop function is used to return the stack and return the value of the exited element
3. The top function is used to obtain the top element of the stack

III Queue queue

1. Definition of queue

Queue is a special linear table, which only allows deletion at the front of the table and insertion at the back of the table. Like stack, queue is a linear table with limited operation. The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the head of the queue.

2. Code

import Struct
class Queue(Struct.Struct):
    QueueSpace=1024
    def __init__(self):
        self.__datas=[None]*Queue.QueueSpace
        self.__end=-1
    def __len__(self):
        if(self.__end<0):return 0
        else:
            return self.__end+1
    def __contains__(self, item):
        return self.__datas.__contains__(item)
    def IsNull(self):
        return self.__end<0
    def Clear(self):
        self.__datas=[None]*Queue.QueueSpace
        self.__end=-1

    def Add(self,obj):
        self.__end+=1
        self.__datas[self.__end]=obj
    def Delete(self):
        if(self.__end<0):return False
        data=self.__datas[0]
        del(self.__datas[0])  #Delete this element
        self.__end-=1  #Moving tail index
        return data
    def GetHead(self):
        if(self.__end<0):return None
        return self.__datas[0]

3. Interpretation

1. The add function is similar to the Push function in Stack, but the difference is that the data nodes for adding data and deleting data in Queue are different
2. The delete function is similar to the Pop function in Stack, but by observing the code, you can find that the added element is at the end, the deleted element is at the beginning, and the storage arrangement is moving forward in memory
3.GetHead function gets the first element because the built-in del function is used in the Delete function. After calling the Delete function, self__ Data [0] is the location of the latter element

IV Tree tree

1. Definition of tree

Tree is a kind of data structure. It is a set with hierarchical relationship composed of n(n ≥ 1) finite nodes. It is called "tree" because it looks like an upside down tree, that is, it has its roots up and its leaves down. It has the following characteristics:
Each node has zero or more children; A node without a parent node is called a root node; Each non root node has only one parent node; In addition to the root node, each child node can be divided into multiple disjoint subtrees.

2. Code

import Struct
class TreeNode(Struct.Struct):
    def __init__(self, value, son):
        self.__son = []
        self.__value = value
        for i in son:           
            self.__son.append(i)

    def __len__(self):
        tmp = TreeNode.GetAllNode(self)
        return len(tmp)

    def __contains__(self, item):
        tmp = TreeNode.GetAllNode(self)
        return tmp.__contains__(item)


    def IsNull(self):
        return len(self.__son) == 0

    def Clear(self):
        self.__son = []

    def AddSon(self, obj):
        self.__son.append(obj)

    def DeleteSon(self, obj):
        self.__son.remove(obj)

    def GetSon(self):
        return self.__son

    def GetValue(self):
        return self.__value

    def GetFirstSon(self):
        if (len(self.__son) == 0):
            return None
        else:
            return self.__son[0]

    def GetLastSon(self):
        if (len(self.__son) == 0):
            return None
        else:
            return self.__son[len(self.__son) - 1]

    def GetAllNode(point):
        all = []
        if (len(point.GetSon()) == 0):
            all.append(point)
            return all
        else:
            all.append(point)
            for i in point.GetSon():
                for j in TreeNode.GetAllNode(i):
                    all.append(j)
        return all

    def GetAllLeaf(point):
        all = []
        if (len(point.GetSon()) == 0):
            all.append(point)
            return all
        else:
            for i in point.GetSon():
                for j in TreeNode.GetAllLeaf(i):
                    all.append(j)
        return all

3. Interpretation

1. When addson function is called, a specified child node can be added to this node
2.GetSon function is used to obtain all child nodes of this node
3.GetValue function is used to obtain the weight of this node
4.GetFirstSon function is used to get the first child node added
5.GetLastSon function is used to get the last child node added
6. The static function GetAllNode recursively obtains all child nodes
7. The static function GetAllLeaf recursively obtains all the child leaf nodes

------More content is constantly updated------

Topics: Python data structure