CSC148H5 Study Guide - Midterm Guide: Init, Tree Traversal, Preorder

135 views8 pages
24 Jan 2017
School
Course

Document Summary

# a binary tree (bt) is none or a list of three elements def binary_tree(value): Creates a node that that has value as it"s value and none as it"s left and right tree. return [value, none, none] def insert_left(bt, value): # think about using append def contains(bt, value): Return true i value is contained in bt. >>> contains([5, [4, none, none], [3, [2, none, none], none], 2) False if not bt: return false return bt[0] == value or contains(bt[1], value) \ or contains(bt[2], value) # note: in class, i printed the preorder values rather than making a list. # i changed it to a list here so that the cool "contains" >>> preorder([5, [4, none, none], [3, none, none]] [5, 4, 3] if not bt: return [] return [bt[0]] + preorder(bt[1]) + preorder(bt[2]) def contains2(bt, value): Binarytree(value) class binarytree: def __init__(self, value): self. key = value self. left = none self. right = none def insert_left(self, 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

Related Documents