python pyQt5 二叉树可视化
时间: 2023-05-30 13:01:54 浏览: 324
以下是一个简单的使用PyQt5实现二叉树可视化的示例代码:
1. 安装PyQt5库:
```
pip install PyQt5
```
2. 导入必要的库:
```python
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QPainter, QFont, QPen
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsItem, QGraphicsTextItem
```
3. 定义节点类:
```python
class Node(QGraphicsItem):
def __init__(self, value, x=0, y=0):
super().__init__()
self.value = value
self.left = None
self.right = None
self.x = x
self.y = y
self.width = 40
self.height = 30
self.pen = QPen(Qt.black)
self.brush = Qt.white
def boundingRect(self):
return QRectF(self.x, self.y, self.width, self.height)
def paint(self, painter, option, widget):
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawRect(self.x, self.y, self.width, self.height)
font = QFont('Arial', 12)
painter.setFont(font)
painter.drawText(self.boundingRect(), Qt.AlignCenter, str(self.value))
```
4. 定义二叉树类:
```python
class BinaryTree():
def __init__(self):
self.root = None
def insert(self, value):
if not self.root:
self.root = Node(value)
else:
self._insert(value, self.root)
def _insert(self, value, node):
if value < node.value:
if node.left:
self._insert(value, node.left)
else:
node.left = Node(value, node.x - 50, node.y + 50)
else:
if node.right:
self._insert(value, node.right)
else:
node.right = Node(value, node.x + 50, node.y + 50)
def traverse(self):
if self.root:
self._traverse(self.root)
def _traverse(self, node):
if node.left:
self._traverse(node.left)
print(node.value)
if node.right:
self._traverse(node.right)
```
5. 定义图形场景类:
```python
class GraphicsScene(QGraphicsScene):
def __init__(self, parent=None):
super().__init__(parent)
self.setSceneRect(0, 0, 500, 500)
self.nodes = []
def addNode(self, node):
self.addItem(node)
def drawLine(self, parent, child):
self.addLine(parent.x + parent.width / 2, parent.y + parent.height, child.x + child.width / 2, child.y)
```
6. 定义主窗口类:
```python
class MainWindow(QGraphicsView):
def __init__(self):
super().__init__()
self.setWindowTitle('Binary Tree Visualization')
self.setRenderHint(QPainter.Antialiasing)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setScene(GraphicsScene())
self.tree = BinaryTree()
self.tree.insert(50)
self.tree.insert(30)
self.tree.insert(70)
self.tree.insert(20)
self.tree.insert(40)
self.tree.insert(60)
self.tree.insert(80)
self.drawTree()
def drawTree(self):
self.drawNode(self.tree.root)
self.drawLines(self.tree.root)
def drawNode(self, node):
scene = self.scene()
scene.addNode(node)
scene.nodes.append(node)
if node.left:
self.drawNode(node.left)
if node.right:
self.drawNode(node.right)
def drawLines(self, node):
scene = self.scene()
if node.left:
scene.drawLine(node, node.left)
self.drawLines(node.left)
if node.right:
scene.drawLine(node, node.right)
self.drawLines(node.right)
```
7. 运行主窗口:
```python
if __name__ == '__main__':
app = QApplication([])
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
```
以上代码可以实现一个简单的二叉树可视化,可以根据需要进行修改和扩展。
阅读全文