树节点
Inserting an element into a tree
Deleting an element from a tree
searching for an element
Traversing the tree
finding the size of the tree
finding the height of the tree
find level
class BTNode:
__slots__ = 'val', 'left', 'right'
#BTNODE属性
def __init__(self, val, left=None, right=None):
#初始化值
self.val = val
self.left = left
self.right = right
left = BTNode(10)
right = BTNode(20)
parent = BTNode(30)
parent.left = left
parent.right = right
#节点联系起来
print('parent (30):', parent.val)
print('left (10):', parent.left.val)
print('right (20):', parent.right.val)
树的类
class BST:
__slots__ = 'root', 'size'#变量有根,还有数量
def __init__(self):#初始化
self.root = None
self.size = 0
def __insert(self, val, node):#小于VAL的放在左边
#要插入的值,以及上一个NODE
if val < node.val:
# check if need to go left
if node.left == None: # if no left child
node.left = BTNode(val) # insert it here
else: # otherwise
self.__insert(val, node.left)
# traverse with the left node#一直找到下面为空的
else: # need to go right
if node.right == None: # if no right child
node.right = BTNode(val) # insert it here
else: # otherwise
self.__insert(val, node.right) # traverse with the right node
def insert(self, val):
"""
Insert a new value into the tree
:param val: The value to insert
:return: None
"""
if self.root == None: # if tree is empty
self.root = BTNode(val) # create root node with the value
else: #节点不为空就分2个情况
self.__insert(val, self.root) # call helper function with root
self.size += 1
def __contains(self, val, node):
if node == None: # if there is no node
return False # we went past a leaf and the val is not there
elif val == node.val: # if the values match
return True # return success
elif val < node.val: # if no match, but val is lesser
return self.__contains(val, node.left) # recurse with left node
else: # otherwise
return self.__contains(val, node.right) # recurse with right node
def contains(self, val):
# call the recursive helper function with the root node
return self.__contains(val, self.root)
def __height(self, node):
if node == None: # if no node
return -1 # the height is -1
else: # otherwise
# add 1 to the greater of the left or right node's height
return 1 + max(self.__height(node.left), self.__height(node.right))
def height(self):
"""
Return the height of a tree. Recall:
- The height of an empty tree is -1
- The height of a tree with one node is 0
- Otherwise the height is one plus the larger of the heights of
the left or right children.
:return: The height (int)
"""
# just call the recursive helper function with the root node
return self.__height(self.root)
def __inorder(self, node):
if node == None: # if we went past a leaf
return ' ' # append a space
else: # otherwise
# construct a string in order from left to current to right
return self.__inorder(node.left) + \
str(node.val) + \
self.__inorder(node.right)
def __str__(self):
"""
Return a string representation of the tree. By default this will
be a string with the values in order.
:return:
"""
# call the recursive helper function with the root node
return self.__inorder(self.root)
Traversing the tree
def preorder(node):
"""
A preorder traversal has a visitation order of parent,
left and then right.
:param node: The current node in the traversal (BTNode)
:return: None
"""
if node != None:
print(node.val, end=' ')
preorder(node.left)
preorder(node.right)
def inorder(node):
"""
An inorder traversal has a visitation order of left,
parent and then right.
:param node: The current node in the traversal (BTNode)
:return: None
"""
if node != None:
inorder(node.left)
print(node.val, end=' ')
inorder(node.right)
def postorder(node):
"""
A postorder traversal has a visitation order of left,
right and then parent.
:param node: The current node in the traversal (BTNode)
:return: None
"""
if node != None:
postorder(node.left)
postorder(node.right)
print(node.val, end=' ')