CSC148H1 Lecture : Exercise 4 A answers These are the answers for part A of the fourth exercise for study purposes. This is the way i did them so i'm sure there are other ways that are possibly better than mine but these are correct. ***Make sure you download the Node fi

32 views1 pages
16 Oct 2011
School
Course
Professor
katrinasavvy and 38715 others unlocked
CSC148H1 Full Course Notes
1
CSC148H1 Full Course Notes
Verified Note
1 document

Document Summary

""" blah """ if preorder == [] or inorder == []: return none root_node = node(preorder[0]) preorder. remove(preorder[0]) inorder_left = inorder[:inorder. index(root_node. key)] inorder_right = inorder[inorder. index(root_node. key) + 1:] if (inorder_left == [] and inorder_right == []) or len(preorder) == 0: return root_node. # walk left if preorder[0] in inorder_left: root_node. left = make_tree(preorder, inorder_left) if len(preorder) == 0: return root_node. # walk right if preorder[0] in inorder_right: root_node. right = make_tree(preorder, inorder_right) return root_node if __name__ == "__main__": #inorder = ["a", "b", "c", "d", "e"] preorder = [10, 6, 8, 12, 11, 15] inorder = [8, 6, 12, 10, 11, 15]

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

Related Questions