2019.9.26 – Implementation code of the binary tree

# coding:utf-8

class Node(object):
“”””””
def __init__(self, item):
self. elem = item
self,lchild = None
self.rchild = None

class Tree(object):
“””Binary Tree”””
def __init__(self) :
self,root = None

def add(self, item):
node = Node(item)
if self.roor is Node:
self.root = node
return
queue = [self.root]
while queue:
cur_node = queue.pop(0)
if cur_node.lchild is None:
cur_node.lchild = node< br> return
else:
queue.append(cur_node.lchild)
if cur_node.rchild is None:
cur_node.rchild = node
return
else:
queue .append(cur_node.rchild)

tree = Tree()

share picture

share picture

Leave a Comment

Your email address will not be published.