Binary Search Trees: Implementing Maps as Trees, Lecture notes of Programming Abstractions

The implementation of a map data structure using a binary search tree. The concepts of binary trees, binary search trees, tree terminology, and the advantages of using a binary search tree over a vector for efficient search and insert operations. The document also includes examples of tree traversals and the implementation of adding a new node to a binary search tree.

Typology: Lecture notes

2010/2011

Uploaded on 10/02/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Admin
Today’s topics
Binary search trees, implementing Map as tree
Reading
Ch 13
Lec ture #22
Map as Vector
!!!!! Unsorted Sorted
Map() O(1) O(1)
~Map() O(1) O(1)
add() O(N) O(N)
getValue() O(N) O(logN)
Overhead per entry none none
A different strategy
Sorting the Vector
Provides fast lookup, but still slow to insert (because of shuffling)
Does a linked list help?
Easy to insert, once at a position
But hard to find position to insert...
Will rearranging pointers help?
Bashful Doc Dopey Grumpy Happy Sleepy Sneezy
Bashful Doc Dopey Grumpy Happy Sleepy Sneezy
Bashful Doc Dopey Grumpy Happy Sleepy Sneezy
Voila... a binary search tree!
Tree terminology
Node, tree, subtree, parent, child, root, leaf
Bashful
Doc
Dopey
Grumpy
Happy
Sleepy
Sneezy
pf3
pf4
pf5

Partial preview of the text

Download Binary Search Trees: Implementing Maps as Trees and more Lecture notes Programming Abstractions in PDF only on Docsity!

Admin

Today’s topics

• Binary search trees, implementing Map as tree

Reading

• Ch 13

Lecture

Map as Vector

!!!!! Unsorted Sorted Map() O(1) O(1) ~Map() O(1) O(1) add() O(N) O(N) getValue() O(N) O(logN) Overhead per entry none none

A different strategy

Sorting the Vector

• Provides fast lookup, but still slow to insert (because of shuffling)

Does a linked list help?

• Easy to insert, once at a position

• But hard to find position to insert...

• Will rearranging pointers help?

Bashful Doc Dopey Grumpy Happy Sleepy Sneezy Bashful Doc Dopey Grumpy Happy Sleepy Sneezy Bashful Doc Dopey Grumpy Happy Sleepy Sneezy

Voila... a binary search tree!

Tree terminology

• Node, tree, subtree, parent, child, root, leaf

Bashful Doc Dopey Grumpy Happy Sleepy Sneezy

Trees in general Rules for all trees

  • Recursive branching structure
  • Single root node
  • Every node reachable from root by unique path Examples
  • Game tree
  • Family tree
  • Filesystem hierarchy
  • Decomposition tree
  • Binary, ternary, n-ary
  • Binary search tree

handouts

cs

~julie

cs

exams slides

Mon

exams

Wed

midterm

personal

Binary search tree in specific Binary tree

  • Each node has at most 2 children Binary search tree
  • Arranged for efficient search/insert
  • All nodes in left subtree are less than root, all nodes in right subtree are greater

struct node { int val; node *left, *right; }; Operating on trees Many tree algorithms are recursive

  • Not suprisingly!
  • Handle current node, recur on subtrees
  • Base case is empty tree (NULL) Tree traversals to visit all nodes - Handle cur node, visit left/right subtrees Whether current node before/after its subtrees determines order of traversal
  • Pre: cur, left, right
  • In: left, cur, right
  • Post: left, right, cur
  • Others: level-by-level, reverse orders, etc. Tree traversals at work // INORDER void PrintTree(node *t) { if (t != NULL) { PrintTree(t->left); cout << t->key << endl; PrintTree(t->right); } } // POSTORDER void FreeTree(node *t) { if (t != NULL) { FreeTree(t->left); FreeTree(t->right); delete t; } }

Map implementation template void Map::add(string key,ValType val) // add is wrapper { treeEnter(root, key, val); // call rec helper to do enter } Recursive treeEnter template void Map::treeEnter(node * & t, string key, ValType val) { if (t == NULL) { t = new node; t->key = key; t->value = val; t->left = t->right = NULL; } else if (key == t->key) { t->value = val; } else if (key < t->key) { treeEnter(t->left, key, val); } else { treeEnter(t->right, key, val); } } root t value peach value pear value papaya value kiwi value banana value apple value melon Trace treeEnter value orange Insert new node Evaluate Map as tree Space used

  • Overhead of two pointers per entry (typically 8 bytes total)
  • Tree adds nodes as needed, no excess capacity maintained Runtime performance
  • Add/getValue take time proportional to tree height
  • Height^ expected^ to be O(logN)

A balanced tree Values: 2 8 14 15 18 20 21 Different trees possible, depends on order inserted 7 nodes, expected height lg7! 3 Perfectly balanced 8 15 20 (^2 14 ) Entered: 15 8 2 20 21 14 18 (one possibility) Mostly balanced trees Same values: 2 8 14 15 18 20 21 Mostly balanced, height 4 or 5

Entered: 20 8 21 18 14 15 2

(one possibility)

Entered: 18 14 15 8 2 20 21

(one possibility)

Degenerate trees Same values: 2 8 14 15 18 20 21 Totally unbalanced, height = 7 Entered: 2 8 14 15 18 20 21 (only possibility)

Entered: 21 20 18 15 14 8 2 (only possibility)

Even more degenerate trees What is relationship between worst-case inputs for tree insertion and Quicksort? Entered: (^21 2 20 8 14 15 18) Entered: 2 8 21 20 18 14 15