CSC148H5 Lecture 16: Binary Trees
Document Summary
Csc148h5s - introduction to computer science (winter 2017) # a binary tree (bt) is none or a list of three elements def binary_tree (value): Return new bt with value as root and no children return [value, none , none ] def insert_left (bt, value): Insert value as the left node of the root of bt. if not bt: left_brach = bt. pop( 1 ) bt. insert( 1 , [value, left_branch, none ]) def insert_right (bt, value): raise valueerror( "cannot insert into empty tree" ) raise valueerror( "cannot insert into empty tree" ) Insert value into the right subtree of t. if not bt: right_branch = pt. pop( 2 ) bt. append([value, none , right_branch]) # we are inserting into the right subtree. # and making the old right subtree be the right subtree. # [4, none, [10, none, [5, none, none]]] def contains (bt, value):