#!/anaconda3 python3.8
# -*- coding: utf-8 -*-
# @File: 24_BST.py
# @Author: zyping1989
# @Time: 2021/3/1 17:37
class BiTreeNode:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None
self.parent = None
class BST:
def __int__(self, li = None):
self.root = None
if li:
for var in li:
self.insert_no_rec(var)
def insert_no_rec(self, val):
p = self.root
if not p: # 空树特殊处理
self.root = BiTreeNode(val)
return
while True:
if val < p.data:
if p.lchild:
p = p.lchild
else: # 左孩子不存在
p.lchild = BiTreeNode(val)
p.lchild.parent = p
return
elif val > p.data:
if p.rchild:
p = p.rchild
else: # 右孩子不存在
p.rchild = BiTreeNode(val)
p.rchild.parent = p
return
else:
return
tree = BST([4, 6, 7, 9, 2, 1, 3, 5, 8])
运行之后提示:TypeError:BST() takes no arguments.
请问各位,这是什么原因呢?
C:\Users\ypzh\Anaconda3\python.exe C:/Users/ypzh/Desktop/123.py
Traceback (most recent call last):
File "C:/Users/ypzh/Desktop/123.py", line 44, in <module>
tree = BST([4, 6, 7, 9, 2, 1, 3, 5, 8])
TypeError: BST() takes no arguments
Process finished with exit code 1