Python数据结构之栈、队列及二叉树定义与用法浅析

时间:2022-08-26 23:35:30

本文实例讲述了python数据结构之栈、队列及二叉树定义与用法。分享给大家供大家参考,具体如下:

目前只实现了三种,栈、队列和二叉树,哪天得空继续补吧~

1. 栈

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#栈
class stack:
  def __init__(self,size = 16):
    self.stack = []
    self.size = size
    self.top = -1
  def setsize(self, size):
    self.size = size
  def isempty(self):
    if self.top == -1:
      return true
    else:
      return false
  def isfull(self):
    if self.top +1 == self.size:
      return true
    else:
      return false
  def top(self):
    if self.isempty():
      raise exception("stackisempty")
    else:
      return self.stack[self.top]
  def push(self,obj):
    if self.isfull():
      raise exception("*")
    else:
      self.stack.append(obj)
      self.top +=1
  def pop(self):
    if self.isempty():
      raise exception("stackisempty")
    else:
      self.top -= 1
      return self.stack.pop()
  def show(self):
    print(self.stack)
s = stack(5)
s.push(1)
s.push(2)
s.push(3)
s.push(4)
s.push(5)
s.show()
s.pop()
s.show()
s.push(6)
s.show()

运行结果:

Python数据结构之栈、队列及二叉树定义与用法浅析

2. 队列

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#队列
class queue:
  def __init__(self,size = 16):
    self.queue = []
    self.size = size
    self.front = 0
    self.rear = 0
  def isempty(self):
    return self.rear == 0
  def isfull(self):
    if (self.front - self.rear +1) == self.size:
      return true
    else:
      return false
  def first(self):
    if self.isempty():
      raise exception("queueisempty")
    else:
      return self.queue[self.front]
  def last(self):
    if self.isempty():
      raise exception("queueisempty")
    else:
      return self.queue[self.rear]
  def add(self,obj):
    if self.isfull():
      raise exception("queueoverflow")
    else:
      self.queue.append(obj)
      self.rear += 1
  def delete(self):
    if self.isempty():
      raise exception("queueisempty")
    else:
      self.rear -=1
      return self.queue.pop(0)
  def show(self):
    print(self.queue)
q = queue(3)
q.add(1)
q.add(2)
q.show()
q.delete()
q.show()

运行结果:

Python数据结构之栈、队列及二叉树定义与用法浅析

3. 二叉树

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#队列
class queue:
  def __init__(self,size = 16):
    self.queue = []
    self.size = size
    self.front = 0
    self.rear = 0
  def isempty(self):
    return self.rear == 0
  def isfull(self):
    if (self.front - self.rear +1) == self.size:
      return true
    else:
      return false
  def first(self):
    if self.isempty():
      raise exception("queueisempty")
    else:
      return self.queue[self.front]
  def last(self):
    if self.isempty():
      raise exception("queueisempty")
    else:
      return self.queue[self.rear]
  def add(self,obj):
    if self.isfull():
      raise exception("queueoverflow")
    else:
      self.queue.append(obj)
      self.rear += 1
  def delete(self):
    if self.isempty():
      raise exception("queueisempty")
    else:
      self.rear -=1
      return self.queue.pop(0)
  def show(self):
    print(self.queue)
#二叉树
class binarytreenode:
  def __init__(self,data,left,right):
    self.left = left
    self.data = data
    self.right = right
class binarytree:
  def __init__(self):
    self.root = none
  def maketree(self,data,left,right):
    self.root = binarytreenode(data,left,right)
    #left.root = right.root = none
  def isempty(self):
    if self.root is none:
      return true
    else:
      return false
  def preorder(self,r):
    if r.root is not none:
      print(r.root.data)
      if r.root.left is not none:
        self.preorder(r.root.left)
      if r.root.right is not none:
        self.preorder(r.root.right)
  def inorder(self,r):
    if r.root is not none:
      if r.root.left is not none:
        self.inorder(r.root.left)
      print(r.root.data)
      if r.root.right is not none:
        self.inorder(r.root.right)
  def postorder(self,r):
    if r.root is not none:
      if r.root.left is not none:
        self.preorder(r.root.left)
      if r.root.right is not none:
        self.preorder(r.root.right)
      print(r.root.data)
  def levelorder(self,a):
    q = queue()
    r = a
    while r is not none:
      print(r.root.data)
      if r.root.left is not none:
        q.add(r.root.left)
      if r.root.right is not none:
        q.add(r.root.right)
      if q.isempty():
        print("empty")
        r = none
      else:
        r = q.delete()
r = binarytree()
ra = binarytree()
ra.maketree(2,none,none)
rb = binarytree()
rb.maketree(3,none,none)
r.maketree(1,ra,rb)
print("前序遍历")
r.preorder(r)
print("中序遍历")
r.inorder(r)
print("后序遍历")
r.postorder(r)
print("层级遍历")
r.levelorder(r)

运行结果:

Python数据结构之栈、队列及二叉树定义与用法浅析

后续实现了会慢慢补上~~旧的也会不断改进~~

希望本文所述对大家python程序设计有所帮助。

原文链接:https://blog.csdn.net/wklken/article/details/6315549