定义二叉搜索树节点类 class TreeNode: def init(self, val): self.val = val self.left = None self.right = None # 插入节点 def insert(root, val): if not root: return TreeNode(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root # 顺序遍历二叉搜索树 def inorderTraversal(root): res = [] if not root: return res res += inorderTraversal(root.left) res.append(root.val) res += inorderTraversal(root.right) return res # 将二叉搜索树转换为双向循环链表 def treeToDoublyList(root): if not root: return None inorder = inorderTraversal(root) head = cur = TreeNode(None) for val in inorder: cur.right = TreeNode(val) cur.right.left = cur cur = cur.right head.right.left = cur cur.right = head.right return head.right # 打印二叉搜索树 def printTree(root, space): if not root: return space += 5 printTree(root.right, space) print(' ' * space, root.val) printTree(root.left, space) # 打印双向循环链表 def printList(head): if not head: return print(head.val, end=' <--> ') cur = head.right while cur != head and cur.right != head: print(cur.val, end=' <--> ') cur = cur.right if cur.right == head: print(cur.val) cur = cur.right length = 7 * len(values) - 7 print('^' + ' '*length + '^') print('|' + ' '*length + '|') print('\' + '-'*length + '/') print() # 测试 if name == 'main': root = None values = input('请输入要插入的节点值,以空格分隔:').split() for val in values: root = insert(root, val) bstree = root print('Binary Search Tree:') printTree(bstree, 0) dllist = treeToDoublyList(bstree) print('Doubly Linked List:') printList(dllist) print('------------------------menu--------------------------- \n' '|0.escape \n' '|1.input the BSTree elements \n' '|2.traverse the BSTree \n' '|3.print the LinkedList \n' '|4.output all the elements by forward and backward order\n')将menu函数的作用融入代码中
时间: 2024-02-15 12:01:21 浏览: 89
好的,以下是将menu函数融入代码中的版本:
```python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(root, val):
if not root:
return TreeNode(val)
if val < root.val:
root.left = insert(root.left, val)
else:
root.right = insert(root.right, val)
return root
def inorderTraversal(root):
res = []
if not root:
return res
res += inorderTraversal(root.left)
res.append(root.val)
res += inorderTraversal(root.right)
return res
def treeToDoublyList(root):
if not root:
return None
inorder = inorderTraversal(root)
head = cur = TreeNode(None)
for val in inorder:
cur.right = TreeNode(val)
cur.right.left = cur
cur = cur.right
head.right.left = cur
cur.right = head.right
return head.right
def printTree(root, space):
if not root:
return
space += 5
printTree(root.right, space)
print(' ' * space, root.val)
printTree(root.left, space)
def printList(head):
if not head:
return
print(head.val, end=' <--> ')
cur = head.right
while cur != head and cur.right != head:
print(cur.val, end=' <--> ')
cur = cur.right
if cur.right == head:
print(cur.val)
cur = cur.right
def menu():
while True:
choice = input('请输入您的选择:')
if choice == '0':
break
elif choice == '1':
values = input('请输入要插入的节点值,以空格分隔:').split()
for val in values:
root = insert(root, val)
bstree = root
print('Binary Search Tree:')
printTree(bstree, 0)
elif choice == '2':
inorder = inorderTraversal(root)
print('Inorder Traversal:')
print(inorder)
elif choice == '3':
dllist = treeToDoublyList(root)
print('Doubly Linked List:')
printList(dllist)
elif choice == '4':
inorder = inorderTraversal(root)
forward = 'Forward Order: ' + ' <--> '.join(inorder)
backward = 'Backward Order: ' + ' <--> '.join(inorder[::-1])
print(forward)
print(backward)
else:
print('无效的选项,请重新选择!')
if __name__ == '__main__':
root = None
menu()
```
这个版本中,我们将menu函数放在了主函数外面,并且使用了一个while循环,让用户可以在一个菜单中进行多次操作。在用户选择了不同的选项之后,代码会执行相应的函数并输出结果。
阅读全文