Binary Search Tree (BST) 15 < > 7 17 3 9 19 What is - - PowerPoint PPT Presentation

binary search tree bst
SMART_READER_LITE
LIVE PREVIEW

Binary Search Tree (BST) 15 < > 7 17 3 9 19 What is - - PowerPoint PPT Presentation

Binary Search Tree (BST) 15 < > 7 17 3 9 19 What is the 1 4 8 11 inorder traversal of a BST? 23 BST Definition BST is a binary tree Each node stores a value from an ordered domain, e.g., numbers or strings The value of any


slide-1
SLIDE 1

Binary Search Tree (BST)

23

3 1 4 9 8 11 15 17 7

< >

What is the inorder traversal

  • f a BST?

19

slide-2
SLIDE 2

BST Definition

BST is a binary tree Each node stores a value from an ordered domain, e.g., numbers or strings The value of any node is ≥ all values in the left subtree, and ≤ all values in the right subtree

24

slide-3
SLIDE 3

BST ADT

Initialize() – Initializes an empty BST Find(x) – Searches for x in the tree and returns true or false Insert(x) – Inserts the value x into the BST Delete(x) – Deletes the value x from the tree if it is in the tree FindMin() – Returns the minimum value in the tree without deleting it FindMax() – Returns the maximum value in the tree without deleting it

25

slide-4
SLIDE 4

BST.Find

26

3 1 4 9 8 11 15 17 7

Find 8 Find 14

19

slide-5
SLIDE 5

BST.Find

27

bool find(Node* node, T x) { if (node == null) return false; if (x == node.value) return true; if (x < node.value) find(node.____, x); if (x > node.value) find(node.____, x); }

slide-6
SLIDE 6

BST.Find

28

bool find(Node* node, T x) { if (node == null) return false; if (x == node.value) return true; if (x < node.value) find(node.left, x); if (x > node.value) find(node.right, x); } FindMin() FindMax()

slide-7
SLIDE 7

BST.Insert

29

3 1 4 9 8 11 15 17 7

Insert 18

19 18

slide-8
SLIDE 8

BST.Insert

30

3 1 4 9 8 11 15 17 7

Insert 14

19 18 14

slide-9
SLIDE 9

BST.Remove

31

3 1 4 9 8 11 15 17 7

Remove 11

19

slide-10
SLIDE 10

BST.Remove

32

3 1 4 9 8 15 17 7

Remove 9

19

slide-11
SLIDE 11

BST.Remove

33

3 1 4 9 8 11 15 17 7

Remove 7

19

slide-12
SLIDE 12

BST.Remove

34

3 1 4 9 8 11 15 17

Remove 7

19

slide-13
SLIDE 13

BST.Remove

35

3 1 4 9 8 11 15 17

Remove 7

19

slide-14
SLIDE 14

BST.Remove

36

3 1 4 9 11 15 17 8

Remove 7

19

slide-15
SLIDE 15

BST.Remove

37

3 1 4 9 11 15 17 8

Remove 7

19

slide-16
SLIDE 16

Analysis of BST Algorithms

Operations:

Find Insert FindMin FindMax Delete

All O(h) where h is the height of the tree h = O(n) (worst case) h = Ω(log n) (best case) How to guarantee O(log n) performance?

38