trees 3 the binary search tree section 4.3. binary search tree also known as totally ordered tree...

22
Trees 3 The Binary Search Tree Section 4.3

Upload: allison-parks

Post on 06-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Binary Search Tree Which one is NOT a BST?

TRANSCRIPT

Page 1: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

Trees 3The Binary Search Tree

Section 4.3

Page 2: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

Binary Search Tree Also known as Totally Ordered Tree

Definition: A binary tree B is called a binary search tree iff: There is an order relation ≤ defined for the vertices of B For any vertex v, and any descendant u of v.left, u ≤ v For any vertex v, and any descendent w of v.right, v ≤ w

Page 3: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

Binary Search Tree

Which one is NOT a BST?

Page 4: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

Binary Search Tree

Consequences: The smallest element in a binary search tree (BST) is the “left-

most” node

The largest element in a BST is the “right-most” node

Inorder traversal of a BST encounters nodes in increasing order

4

2 6

1 3 75

root

Page 5: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

Binary Search using BST

Assumes nodes are organized in a totally ordered binary tree Begin at root node Descend using comparison to make left/right decision

if (search_value < node_value) “go to the left child”

else if (search_value > node_value) “go to the right child”

else return true // success Until descending move is impossible Return false (failure)

Page 6: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST Class Template

Page 7: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST Class Template (contd.)

Internal functionsused in recursive calls

Pointer passed by reference (why?)

Page 8: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Public members calling private recursive functions

Page 9: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Searching for an element

Page 10: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Search using function objects

Page 11: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Find the smallest element

Tail recursion

Page 12: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Find the biggest element

Non-recursive

Page 13: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Insertion

Before insertionAfter insertion

Page 14: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Insertion (contd.)

Strategy:

•Traverse the tree as in searching for t with contains()

•Insert if you cannot find the element t.

Page 15: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Deletion

Before delete(4)

After delete(4)

Deleting a node with one child

Deletion Strategy: Bypass the node being deleted

Page 16: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Deletion (contd.)

Before delete(2)

After delete(2)

Deleting a node with two children

Deletion Strategy: Replace the node with smallest node in the right subtree

Page 17: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Deletion (contd.)

Page 18: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Lazy Deletion

Another deletion strategy Don’t delete! Just mark the node as deleted. Wastes space But useful if deletions are rare or space is not a

concern.

Page 19: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Destructor

Page 20: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Assignment Operator

Page 21: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Insertion Bias

Start with an empty tree.Insert elements in sorted orderWhat tree do you get?How do you fix it?

Page 22: Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search

BST: Deletion Bias

After large number of alternating insertions and deletions

Why this bias? How do you fix it?