Binary Search Trees in CSE 143: A Comprehensive Guide, Schemes and Mind Maps of Data Structures and Algorithms

Finding a value in a binary tree potentially means visiting every node. • Searching a sorted array would still be faster (via binary search).

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 03/01/2023

anshula
anshula 🇺🇸

4.4

(12)

243 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
X
3/9/00 X-1
CSE 143
Binary Search Trees
[Chapter 10]
3/9/00 X-2
A Problem
Finding a value in a binary tree potentially means
visit ing every node
Searching a sorted array would still be faster (via
binary s earch)
If we imposed some ordering on the tree, maybe
we could speed things up...
Leads to the concept of a binary search tree (BST)
3/9/00 X-3
Binary Search Trees (BST)
Sorti ng constraints: fo r every node v,
All data in left subtree of v < value of v
All data in right subt ree of v > value of v
Note: no dupl icate values
A binary tree with these constraints is called a
binary search tree (BST)
Prerequisite: The items must have a concept of
< and >
Does this limit us to ints, doubles, etc.?
No! In C++, we can use operat or overloading t o define
<, > etc. for any class. 3/9/00 X-4
BSTs May Not Be Unique
Given a set of values, there could be many
possible BSTs
3/9/00 X-5
Examples and Non-Examples
9
4
27
3681
12
15
14
9
8
27
3561
12
15
14
A Binary Search Tree Not a Binary Search Tree
3/9/00 X-6
Finding an item in a BST
9
4
27
3681
12
15
14
find(root, 6)
root
9
4
27
3681
12
15
14
find(root, 10)
root
NULL
pf3
pf4

Partial preview of the text

Download Binary Search Trees in CSE 143: A Comprehensive Guide and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

3/9/ X-

CSE 143

Binary Search Trees

[Chapter 10]

3/9/ X-

A Problem

  • Finding a value in a binary tree potentially means

visiting every node

  • Searching a sorted array would still be faster (via

binary search)

  • If we imposed some ordering on the tree, maybe

we could speed things up...

  • Leads to the concept of a binary search tree (BST)

3/9/00 (^) X-

Binary Search Trees (BST)

  • Sorting constraints: for every node v,
    • All data in left subtree of v < value of v
    • All data in right subtree of v > value of v
    • Note: no duplicate values
  • A binary tree with these constraints is called a

binary search tree (BST)

  • Prerequisite: The items must have a concept of

“<“ and “>”

  • Does this limit us to ints, doubles, etc.?
  • No! In C++, we can use operator overloading to define

<, > etc. for any class.

3/9/00 (^) X-

BSTs May Not Be Unique

  • Given a set of values, there could be many

possible BSTs

3/9/ X-

Examples and Non-Examples

12

15

14

12

15

14

A Binary Search Tree Not a Binary Search Tree

3/9/ X-

Finding an item in a BST

12

15

14

find(root, 6)

root

12

15

14

find(root, 10)

root

NULL

3/9/ X-

Code For Finding an Item

If we have a binary search tree, then Find can be done as:

bool find (BTreeNode *root, int item) { if ( root == NULL ) return false; else if (item == root->data) return true; else if (item < root->data) return find (root->left, item); else return find (root->right, item); }

3/9/ X-

Running time of BST find

  • Best case: O(1), item is at root
  • Worst case: O(h), where h is height of tree
  • Leads to a question:
    • What is the height of a binary search tree with N nodes?
  • “Full” tree ( 2 d^ nodes at each depth d) is best case:
    • N = 2 h+1^ - 1
    • h = log 2 (N+1) - 1 = O(log N)
    • logarithmic running time

12

15

root

10

3/9/00 (^) X-

Running time of find (2)

  • What if tree isn’t balanced?
  • Worst case is degenerate tree
    • Height = N, the number of nodes
  • Running time of find, worst-case, is O(N)

12

15

18

root

3/9/00 (^) X-

Inserting in a BST

To insert a new key:

  • Two base cases:
    • If tree is empty, create new node for item
    • If root holds key, return (no duplicate keys allowed)
  • Recursive case: If key < root’s value, (recursively) insert in left subtree, otherwise insert in right subtree

3/9/ X-

Example

Add 8, 10, 5, 1, 7, 11 to an initially empty BST,

in that order:

3/9/ X-

Code For Inserting in a BST

// Add data to tree void insert (BTreeNode * & root, int data) { if ( root == NULL ) { root = new BTreeNode; root->left = NULL; root->right = NULL; root->item = data; return; } if (data < root->item) insert (root->left, data); if (data > root->item ) insert (root->right, data); }

3/9/ X-

Deletion (4): Deleting the Node

void deleteNode (BTreeNode&t) { if (t->left && t->right) { // 2 children t->data = findMin (t->right); deleteItem (t->data, t->right); } else { // 0 or 1 child BTreeNode oldVal = t; if (t->left) // left child only t = t->left; else if (t->right) // right child only t = t->right; else // no children t = NULL; delete oldVal; //delete this node } } 3/9/ X-

Deletion (5): Finding Min

  • All that remains is to figure out how to find the

minimum value in a BST

  • Remember, the minimum element lives at the

leftmost “corner” of a BST

// PRECONDITION: t is non-NULL int findMin (BTreeNode* t) { assert(t != NULL); while (t->left != NULL) t = t->left; return t->data; }

3/9/00 (^) X-

Magic Trick

  • Suppose you had a bunch of numbers, and inserted

them all into an initially empty BST.

  • Then suppose you traversed the tree in-order.
  • The nodes would be visited in order of their

values. In other words, the numbers would come

out sorted!

  • This is TreeSort : another sorting algorithm.
    • O(N log N) most of the time
    • not an “in-place” sort
  • Trivial to program if you already have a BST

ADT. 3/9/00^ X-

Preview of CSE326/373:

Balanced Search Trees

  • BST operations are dependent on tree height
    • O(log N) for N nodes if tree is balanced
    • O(N) if tree is not
  • Can we ensure tree is always balanced?
    • Yes: insert and delete can be modified to keep

the tree pretty well balanced

  • Actually there are several different balanced tree data structures
  • Exact details are complicated
  • Results in O(log N) “find” operations, even in worst

case

3/9/ X-

BST Summary

  • BST = Binary Trees with ordering invariant
  • Recursive BST search
  • Recursive insert, delete functions
  • O(H) operations, where H is height of tree
  • O(log N) for N nodes in balanced case
  • O(N) in worst case