Binary Search Trees: Searching, Insertion, and Deletion, Study notes of Data Structures and Algorithms

An overview of binary search trees (bsts), focusing on searching, insertion, and deletion operations. It includes a sample bst, explanations of predecessors and successors, and java code for an mbst class implementing these operations.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-5kh
koofers-user-5kh 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
19 - 1
Binary Search Trees
PS5 due 11:59pm Wednesday, April 18
Final Project Phase 2 (Program Outline) due 1:30pm Tuesd ay, April 24
Wellesley College CS230
Lecture 19
Thursday, April 12
Handout #30
19 - 2
Overview of Today’s Lecture
Review of Binary Search Trees (BSTs)
Searching, Insertion, and Deletion in BSTs
Implementing Sets, Bags, Tables using BSTs
pf3
pf4
pf5
pf8

Partial preview of the text

Download Binary Search Trees: Searching, Insertion, and Deletion and more Study notes Data Structures and Algorithms in PDF only on Docsity!

19 - 1

Binary Search Trees

PS5 due 11:59pm Wednesday, April 18 Final Project Phase 2 (Program Outline) due 1:30pm Tuesday, April 24

Wellesley College CS

Lecture 19

Thursday, April 12

Handout

19 - 2

Overview of Today’s Lecture

• Review of Binary Search Trees (BSTs)

• Searching, Insertion, and Deletion in BSTs

• Implementing Sets, Bags, Tables using BSTs

19 - 3 To get full advantage of binary trees for data structures, need the values to be ordered. A binary search tree (BST) is a binary tree in which the following BST properties hold at every node: (1) All elements in the left subtree are ≤ the value; (2) All elements in the right subtree are ≥ the value. Here is a sample BST: Binary Search Trees

P

S

V

F

C M

F J

Notes:

  • A BST may contain duplicates
  • The in-order traversal of BST elements

yields them in sorted order. E.g.:

C F F J M P S V

  • For n > 1, there is more than one BST with

n elements.

19 - 4

The predecessor of a node N is the node that immediately precedes N in an

in-order traversal of the tree (or null if no such node exists). E.g., the

predecessor of node P is node M, of node M is node J, and of node J is node

F.

The successor of a node is the node N that immediately follows N in an in-

order traversal of the tree (or null if no such node exists). E.g., the

successor of node F is node J, of node J is node M, and of node M is node P.

Predecessors and Succesors

P

S

V

F

C M

F J

19 - 7

/** Return true if x is in the tree and false otherwise */

public boolean search (T x) {

MBinTree here = elts;

while (! MBT.isLeaf(here)) {

int c = compare(x, MBT.value(here));

if (c == 0) {

return true;

} else if (c < 0) {

here = MBT.left(here);

} else {

here = MBT.right(here);

// Only reach here if got to leaf without finding element:

return false;

Searching in MBST 19 - 8 public void insert (T x) { MBinTree child = elts; MBinTree parent = null; // parent of child boolean isChildToLeft = true; // is child the left of parent? (initialized arbitrarily) while (! MBT.isLeaf(child)) { // search for insertion point int c = compare(x, MBT.value(child)); if (c == 0) throw new MBSTInsertionException(child); // element is already in tree else if (c < 0) { parent = child; child = MBT.left(child); isChildToLeft = true; } else { parent = child; child = MBT.right(child); isChildToLeft = false; } } // Only reach here if got to leaf without finding element: // Replace the leaf in parent by a new node: MBinTree newNode = MBT.node(MBT.leaf(), x, MBT.leaf()); setChild(parent, isChildToLeft, newNode); } Insertion in MBST

19 - 9 private void setChild (MBinTree parent, boolean isChildToLeft, MBinTree newChild) { if (parent == null) { // special case when no parent elts = newChild; } else if (isChildToLeft) { MBT.setLeft(parent, newChild); } else { MBT.setRight(parent, newChild); } } Setting the Child 19 - 10 public boolean delete (T x) { MBinTree child = elts; MBinTree parent = null; // parent of child boolean isChildToLeft = true; // is child the left of parent? (initialized arbitrarily) while (! MBT.isLeaf(child)) { // search for insertion point int c = compare(x, MBT.value(child)); if (c == 0) { deleteNode(parent, child, isChildToLeft); // See next slide return true; // element is in tree. } else if (c < 0) { parent = child; child = MBT.left(child); isChildToLeft = true; } else { parent = child; child = MBT.right(child); isChildToLeft = false; } } // Only reach here if got to leaf without finding element. return false; // element not in tree. } Deletion In MBST

19 - 13 public class FindInfo { public MBinTree parent, child; public boolean isChildToLeft; public FindInfo (MBinTree parent, MBinTree child, boolean isChildToLeft) { this.parent = parent; this.child = child; this.isChildToLeft = isChildToLeft; } } Abstracting over BST search: FindInfo 19 - 14 /** If x exists in tree returns a FindInfo fi where:

  • fi.child is the node containing x;
  • fi.parent is the parent of child (or null if there is no parent);
  • fi.isChildToLeft is true if child is to the left of parent (or there is none); else false If x does not exist in the tree, returns a FindInfo fi where:
  • fi.child is the leaf where x would be inserted into the tree.
  • fi.parent is the node below which x would be inserted into the tree.
  • fi.isChildToLeft is true if x would be inserted to the left of fei.parent; else false */ public FindInfo find (T x) { MBinTree child = elts; MBinTree parent = null; // parent of child boolean isChildToLeft = true; // is child the left of parent? (initialized arbitrarily) int c = 0; // result of comparison (initialized arbitrarily) while (!MBT.isLeaf(child) && // stuff result of comparison in variable c !((c = compare(x, MBT.value(child))) == 0)) { if (c < 0) {parent = child; child = MBT.left(child); isChildToLeft = true;} else {parent = child; child = MBT.right(child); isChildToLeft = false;} } return new FindInfo(parent, child, isChildToLeft); } Abstracting over BST search: find()

19 - 15 public boolean search (T x) { return (!MBT.isLeaf(find(x).child)); } public void insert (T x) { FindInfo fi = find(x); if (MBT.isLeaf(fi.child)) { MBinTree newNode = MBT.node(MBT.leaf(), x, MBT.leaf()); setChild(fi.parent, fi.isChildToLeft, newNode); } else { throw new MBSTInsertionException(fi.child); // element is already in tree } } public boolean delete (T x) { FindInfo fi = find(x); if (!MBT.isLeaf(fi.child)) { deleteNode(fi.parent, fi.child, fi.isChildToLeft); return true; // element is in tree } else { return false; // element not in tree. } } Abstracted search(), insert(), delete()