Khi được yêu cầu tìm giá trị lớn nhất trong cây bằng cách sử dụng theo thứ tự duyệt, một lớp cây nhị phân được tạo với các phương thức để đặt phần tử gốc, thực hiện theo thứ tự duyệt bằng cách sử dụng đệ quy, v.v.
Một thể hiện của lớp được tạo và nó có thể được sử dụng để truy cập các phương thức.
Dưới đây là minh chứng về điều tương tự -
Ví dụ
class BinaryTree_Struct:
def __init__(self, key=None):
self.key = key
self.left = None
self.right = None
def set_root(self, key):
self.key = key
def inorder_traversal_largest(self):
largest = []
self.inorder_largest_helper_fun(largest)
return largest[0]
def inorder_largest_helper_fun(self, largest):
if self.left is not None:
self.left.inorder_largest_helper_fun(largest)
if largest == []:
largest.append(self.key)
elif largest[0] < self.key:
largest[0] = self.key
if self.right is not None:
self.right.inorder_largest_helper_fun(largest)
def insert_to_left(self, new_node):
self.left = new_node
def insert_to_right(self, new_node):
self.right = new_node
def search_elem(self, key):
if self.key == key:
return self
if self.left is not None:
temp = self.left.search_elem(key)
if temp is not None:
return temp
if self.right is not None:
temp = self.right.search_elem(key)
return temp
return None
my_instance = None
print('Menu (this assumes no duplicate keys)')
print('insert <data> at root')
print('insert <data> left of <data>')
print('insert <data> right of <data>')
print('largest')
print('quit')
while True:
my_input = input('What operation would you do ? ').split()
operation = my_input[0].strip().lower()
if operation == 'insert':
data = int(my_input[1])
new_node = BinaryTree_Struct(data)
suboperation = my_input[2].strip().lower()
if suboperation == 'at':
my_instance = new_node
else:
position = my_input[4].strip().lower()
key = int(position)
ref_node = None
if my_instance is not None:
ref_node = my_instance.search_elem(key)
if ref_node is None:
print('No such key exists')
continue
if suboperation == 'left':
ref_node.insert_to_left(new_node)
elif suboperation == 'right':
ref_node.insert_to_right(new_node)
elif operation == 'largest':
if my_instance is None:
print('The tree is empty')
else:
print('The largest element is : {}'.format(my_instance.inorder_traversal_largest()))
elif operation == 'quit':
break Đầu ra
MenuMenu (this assumes no duplicate keys) insert <data> at root insert <data> left of <data> insert <data> right of <data> largest quit What operation would you do ? insert 8 at root What operation would you do ? insert 9 left of 8 What operation would you do ? insert 4 right of 8 What operation would you do ? largest The largest element is : 9 What operation would you do ? > Use quit() or Ctrl-D (i.e. EOF) to exit
Giải thích
-
Lớp 'BinaryTree_struct' với các thuộc tính bắt buộc được tạo.
-
Nó có chức năng 'init' được sử dụng để đặt các nút bên trái và bên phải thành 'Không có'.
-
Nó có phương thức ‘set_root’ giúp thiết lập gốc của cây nhị phân.
-
Một phương thức khác có tên là ‘inorder_traversal_largest’ thực hiện duyệt inorder bằng cách sử dụng đệ quy.
-
Do đó, nó có một chức năng trợ giúp được xác định cùng với.
-
Một phương thức khác có tên là ‘insert_to_right’ được định nghĩa để giúp thêm phần tử vào phía bên phải của nút gốc.
-
Một phương thức có tên ‘insert_to_left’ được xác định, giúp thêm phần tử vào bên trái của nút gốc.
-
Phương thức có tên ‘search_elem’ được xác định, giúp tìm kiếm một phần tử cụ thể.
-
Một đối tượng của lớp ‘BinaryTree_struct’ được tạo.
-
Đầu vào của người dùng được sử dụng cho thao tác cần được thực hiện.
-
Tùy thuộc vào lựa chọn của người dùng, hoạt động được thực hiện.
-
Đầu ra có liên quan được hiển thị trên bảng điều khiển.