Binary Search Trees Data Structures and Algorithms CSE 373 SP 18 - - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

Binary Search Trees Data Structures and Algorithms CSE 373 SP 18 - - - PowerPoint PPT Presentation

Binary Search Trees Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up From CS CSE 143 43: - what is a binary tree - how do you write code to build a tree from scratch? - how do you write code to traverse an


slide-1
SLIDE 1

Binary Search Trees

Data Structures and Algorithms

CSE 373 SP 18 - KASEY CHAMPION 1

slide-2
SLIDE 2

Warm Up

From CS CSE 143 43:

  • what is a “binary tree”
  • how do you write code to build a tree from scratch?
  • how do you write code to traverse an existing tree?
  • how do you write code to change an existing tree?
  • What is the runtime to traverse a tree and print out every node?

Socrative:

www.socrative.com Room Name: CSE373 Please enter your name as: Last, First

CSE 373 SP 18 - KASEY CHAMPION 2

slide-3
SLIDE 3

Storing Sorted Items in an Array

get() – O(logn) put() – O(n) remove() – O(n) Can we do better with insertions and removals?

CSE 373 SP 18 - KASEY CHAMPION 3

slide-4
SLIDE 4

Trees!

A tree is a collection of nodes

  • Each node has at most 1 parent and 0 or more children

Root node: e: the single node with no parent, “top” of the tree Branc nch h node: e: a node with one or more children Leaf f node: e: a node with no children Edge: : a pointer from one node to another Subtree: ee: a node and all it descendants Heig ight: ht: the number of edges contained in the longest path from root node to some leaf node

CSE 373 SP 18 - KASEY CHAMPION 4

1 2 5 3 6 7 4 8

slide-5
SLIDE 5

Tree Height

What is the height of the following trees?

CSE 373 SP 18 - KASEY CHAMPION 5

1 2 5 7 7

  • verallRoot
  • verallRoot
  • verallRoot

null Height = 2 Height = 0 Height = -1 or NA

slide-6
SLIDE 6

Traversals

trave aversal al: An examination of the elements of a tree.

– A pattern used in many tree algorithms and methods

Common orderings for traversals:

– pre-order er: process root node, then its left/right subtrees

– 17 41 29 6 9 81 40

– in in-or

  • rder

er: process left subtree, then root node, then right

– 29 41 6 17 81 9 40

– post-or

  • rder

er: process left/right subtrees, then root node

– 29 6 41 81 40 9 17

Traversal Trick: Sailboat method

– Trace a path around the tree. – As you pass a node on the proper side, process it.

  • pre-order: left side
  • in-order: bottom
  • post-order: right side

CSE 373 SP 17 – ZORA FUNG 6

40 81 9 41 17 6 29

  • verallRoot
slide-7
SLIDE 7

Binary Search Trees

A bina nary y search ch tree e is a binary tree that contains comparable items such that for every node, all children to the left contain smaller data and all children to the right contain larger data.

CSE 373 SP 18 - KASEY CHAMPION 7

10 9 15 7 12 18 8 17

slide-8
SLIDE 8

Implement Dictionary

Binary Search Trees allow us to:

  • quickly find what we’re looking for
  • add and remove values easily

Dictionary Operations: Runtime in terms of height, “h” get() – O(h) put() – O(h) remove() – O(h)

What do you replace the node with? Largest in left sub tree or smallest in right sub tree

CSE 373 SP 18 - KASEY CHAMPION 8

10 “foo” 7 “bar” 12 “baz” 9 “sho” 5 “fo” 15 “sup” 13 “boo” 8 “poo” 1 “burp”

slide-9
SLIDE 9

CSE 373 SP 18 - KASEY CHAMPION 9