30
CS 171: Introduction to Computer Science II Binary Search Trees (cont.)

CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

CS 171: Introduction to Computer Science II

Binary Search Trees (cont.)

Page 2: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Binary Search Trees

• Definitions and terminologies

• Search and insert

• Traversal

• Ordered operations

• Delete• Delete

Page 3: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 4: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 5: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 6: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Traversal

• In-order

–Left subtree, current node, right subtree

• Pre-order

–Current node, left subtree, right subtree

• Post-order• Post-order

–Left subtree, right subtree, current node

Page 7: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 8: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 9: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

• In-order

A C E H M R S X

• Pre-order?

• Post-order?

Traversal

Page 10: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

• In-order

A C E H M R S X

• Pre-order

S E A C R H M X

• Post-order

Traversal

• Post-order

C A M H R E X S

• How to visit the nodes in

descending order?

• What’s the use of pre-order and

post-order traversal?

Page 11: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Expression Tree

• Post-order traversal results in postfix notation

• Pre-order traversal results in prefix notation

Page 12: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Binary Search Trees

• Definitions and terminologies

• Search and insert

• Traversal

• Ordered operations

–Minimum and maximum–Minimum and maximum

–Rank: how many keys < k?

–Select: key of given rank

• Delete

Page 13: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 14: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 15: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 16: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 17: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 18: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Binary Search Trees

• Definitions and terminologies

• Search and insert

• Traversal

• Ordered operations

• Delete• Delete

–Delete minimum and maximum

–Delete a given key

Page 19: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Delete minimum

Page 20: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 21: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 22: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 23: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 24: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 25: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 26: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 27: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 28: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 29: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary
Page 30: CS171:Introduction to Computer Science II Binary Search ... · Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link right child of root A binary

Binary Search Trees

• Definitions and terminologies

• Search and insert

• Traversal

• Ordered operations

• Delete• Delete

• Balanced search trees (Amy Shannon)