先来看问题
这两段都是bst.py里面的代码 AVL.p代码是没问题的
def __init__(self, li=None):
#创建要给根节点
self.root = None
#不是none
if li:
for val in li:
self.insert_no_rec(val)
def insert_no_rec(self, val):
这两个_no_rec 只要是_no_rec 就会报错
AttributeError: 'BiTreeNode' object has no attribute 'bf'
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import random
class BiTreeNode:
def __init__(self, data):
self.data = data
self.lchild = None # 左孩子
self.rchild = None # 右孩子
self.parent = None
class BST:
def __init__(self, li=None):
#创建要给根节点
self.root = None
#不是none
if li:
for val in li:
self.insert_rec(val)
#插入 node是节点 val是值 递归
def insert(self, node, val):
#node是空
if not node:
#只是创建节点,但是没有连接起来 空树,递归不需要特殊出来
node = BiTreeNode(val)
#12 < 17 往左走
elif val < node.data:
#这是父亲连着左孩子 --这是左孩子
node.lchild = self.insert(node.lchild, val)
#这是左孩子的父亲是 --这是父亲
node.lchild.parent = node
#这是右孩子
elif val > node.data:
node.rchild = self.insert(node.rchild, val)
#右孩子连着父亲
node.rchild.parent = node
return node
#非递归
def insert_rec(self, val):
#根节点
p = self.root
#没值
if not p: # 空树
self.root = BiTreeNode(val)
return
while True:
#左子树
if val < p.data:
if p.lchild:
#左子树有 p=左子树,往