二叉插入排序python语言
时间: 2023-12-13 08:34:15 浏览: 66
python实现的二叉树排序
以下是Python实现二叉搜索树BST的插入排序方法示例:
```python
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
class BST:
def __init__(self):
self.root = None
def insert(self, val):
if not self.root:
self.root = Node(val)
else:
self._insert(val, self.root)
def _insert(self, val, cur_node):
if val < cur_node.val:
if not cur_node.left:
cur_node.left = Node(val)
else:
self._insert(val, cur_node.left)
elif val > cur_node.val:
if not cur_node.right:
cur_node.right = Node(val)
else:
self._insert(val, cur_node.right)
else:
print("Value already in tree!")
```
阅读全文