CSC148H5 Lecture 16: Binary Trees

23 views3 pages

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):

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents