TREES
Lecture 11 CS2110 – Summer 2019
TREES Lecture 11 CS2110 Summer 2019 Announcements 2 Confusion - - PowerPoint PPT Presentation
TREES Lecture 11 CS2110 Summer 2019 Announcements 2 Confusion about submission due dates/times can be cleared by reading the syllabus (https://courses.cs.cornell.edu/cs2110/2019su/syll abus.html). Remember to make groups before
Lecture 11 CS2110 – Summer 2019
§
§
§
2
¨ Search for “trees” ¨ Read PDFs for points 0 through 5: intro to trees,
3
¨ Data structure
¤ Organization or format for storing or managing data ¤ Concrete realization of an abstract data type
¨ Operations
¤ Always a tradeoff: some operations more efficient,
¤ Choose efficient data structure for operations of
4
Data Structure add(val v) get(int i) Array Linked List
5
2 1 3 0
2 1 3
contains(val v)
6
7
8
¤ Each node may have zero or
¤ Each node has exactly one
¤ All nodes are reachable
A tree Not a tree Not a tree A tree
9
10
11
12
13
14
General tree Binary tree
15
16
depth 1 2 Height 2, minimum number of nodes Height 2, maximum number of nodes
a binary tree
value left subtree right subtree
value
21
22
23
Binary Tree
Data Structure add(val v) get(int i) Array Linked List
24
2 1 3 0
2 1 3
contains(val v)
2 1 3
25
¨ Binary tree: two recursive calls: O(n) ¨ BST: one recursive call: O(height)
26
27
¤Search for value ¤If not found, put in tree where search ends
28
29
30
31
32
Binary Tree BST
Data Structure add(val x) get(int i) Array Linked List
33
2 1 3 0
2 1 3
contains(val x)
1 2 3
2 1 3
34
35
36
¨ Takeaway: BST search is O(n) time
¤ Recall, big O notation is for worst case running time ¤ Worst case for BST is data inserted in sorted order
¨ Balanced binary tree: subtrees of any node are
¤ In balanced BST, search is O(log n) ¤ Deletion: tricky! Have to maintain balance ¤ [Optional] See JavaHyperText “Extensions to BSTs” ¤ Also see CS 3110
37