1 / 85

CS 132 Spring 2008 Chapter 11

CS 132 Spring 2008 Chapter 11. Binary Trees p. 631-666. Jill’s Pizza Shop. Owner Jill Manager Chef Brad Carl

waylon
Download Presentation

CS 132 Spring 2008 Chapter 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 132 Spring 2008Chapter 11 Binary Trees p. 631-666

  2. Jill’s Pizza Shop Owner Jill Manager Chef Brad Carl Waitress Waiter Cook Helper Joyce Chris Max Len

  3. A Tree Has a Root Node ROOT NODE Owner Jill Manager Chef Brad Carl Waitress Waiter Cook Helper Joyce Chris Max Len

  4. Leaf Nodes Have No Children LEAF NODES Owner Jill Manager Chef Brad Carl Waitress Waiter Cook Helper Joyce Chris Max Len

  5. A Tree Has Levels Owner Jill Manager Chef Brad Carl Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 0 LEVEL 1

  6. A Subtree Owner Jill Manager Chef Brad Carl Waitress Waiter Cook Helper Joyce Chris Max Len LEFT SUBTREE OF ROOT NODE Note: the left subtree is a tree

  7. Another Subtree Owner Jill Manager Chef Brad Carl Waitress Waiter Cook Helper Joyce Chris Max Len RIGHT SUBTREE OF ROOT NODE

  8. Binary Trees (formal definition) • A binary tree, T, is either empty or such that • T has a special node called the root node • T has two sets of nodes: LT (the left subtreeof T) RT (the right subtree of T) • LT and RT are binary trees

  9. Binary Tree Definitions • Leaf: node that has no left and right children • Parent: node with at least one child node • Level of a node: number of steps from root to the node • Height: number of nodes in the longest path from root to a leaf

  10. Binary Trees • A node of a binary tree: • template<class elemType> • struct nodeType • { • elemType info; //data • nodeType<elemType> *llink; //ptr to left child • nodeType<elemType> *rlink; //ptr to right child • }; • Hand out binaryTree.h

  11. Height of a Binary Tree • Recursive algorithm to find height of binary tree with root p: • if(p is NULL) • height(p) = 0 • else • height(p) = 1 + max(height(p->llink), height(p->rlink)) • Recursive function: • template<class elemType> • int height(nodeType<elemType> *p) • { • if(p == NULL) • return 0; • else • return 1 + max(height(p->llink), height(p->rlink)); • }

  12. Binary Tree Traversal • Visit the root node first or visit the subtrees first • inorder • traverse the left subtree • visit the node • traverse the right subtree • preorder • visit the node • traverse the left subtree • traverse the right subtree • postorder • traverse the left subtree • traverse the right subtree • visit the node

  13. Binary TreeTraversal • Inorder: • B D A C • Preorder: • A B D C • Postorder: • C D B A

  14. Binary Tree: Traversals template<class elemType> void postorder(nodeType<elemType> *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout<<p->info<<” “; } }1 template<class elemType> void inorder(nodeType<elemType> *p) { if(p != NULL) { inorder(p->llink); cout<<p->info<<” “; inorder(p->rlink); } } What about preorder?

  15. Traversal of Binary Expression Trees • What would be the effect of traversal inorder preorder postorder • More later * a b

  16. A Binary Search Tree (BST) • A special kind of binary tree in which: • 1. each node contains a distinct data value, • 2. the key values in the tree can be compared using “greater than” and “less than”, and • 3. the key value of each node in the tree is • less than every key value in its right subtree, and • greater than every key value in its left subtree What would inorder traversal produce?

  17. Q E S L T A K v Binary Search Trees What would inorder traversal produce?

  18. Operations on Binary Search Trees • Search for a particular item • How do you know if 46 is in a tree? • is it the root? • no, less; • go left ...

  19. How Do You Know If 46 Is In A Tree? Is it the root? No, less; go left ...

  20. template<class elemType> • bool bSearchTreeType<elemType>::search(const elemType& searchItem) • { • nodeType<elemType> *current; • bool found = false; • if(root == NULL) • cerr<<"Cannot search the empty tree."<<endl; • else • { • current = root; • while(current != NULL && !found) • { • if(current->info == searchItem) • found = true; • else • if(current->info > searchItem) • current = current->llink; • else • current = current->rlink; • }//end while • }//end else • return found; • }//end search • Is there another way?

  21. Binary Search Tree Analysis Worst Case: linear tree n nodes: n steps

  22. Binary Search Tree Analysis • Best case: a balanced tree Nodes Steps 1 1 3 2 7 3 15 4 2n – 1 n

  23. ‘J’ Creating Binary Search Tree • Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. • Insert the first value into the root node:

  24. ‘J’ Inserting ‘E’ into the BST • Thereafter, each value to be inserted begins by • comparing itself to the value in the root node • moving left it is less, or moving right if it is greater • continuing down until it can be inserted as a new leaf • E is less than J, go left: ‘E’

  25. ‘J’ ‘E’ Inserting ‘F’ into the BST • Compare ‘F’ to the value in the root node • Move left because it is less • Compare ‘F’ to the left child node value, move right • We reached the bottom, insert the node ‘F’ new node

  26. ‘J’ ‘T’ ‘E’ ‘F’ Inserting ‘T’ into the BST • Compare ‘T’ to the value in the root node ...

  27. ‘J’ ‘T’ ‘E’ ‘A’ Inserting ‘A’ into the BST • Compare ‘A’ to the value in the root node ... ‘F’

  28. ‘J’ Another Binary Search Tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘P’ ‘K’ Add nodes for these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

  29. ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ Creating a Binary Search Tree • What BST is obtained by inserting elements • ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?

  30. AVL (Height-balanced Trees) • A perfectly balanced binary tree is a binary tree such that: • The height of the left and right subtrees of the root are equal • The left and right subtrees of the root are perfectly balanced binary trees

  31. AVL (Height-balanced Trees) • An AVL tree (or height-balanced tree) is a binary search tree such that: • the height of the left and right subtrees of the root differ by at most 1 • the left and right subtrees of the root are AVL trees • Goal: maintain a BST as AVL • Approach: rebalance unbalanced • trees every now and then • Us: skip this part

  32. Full and Complete Binary Trees • A full binary tree: all the leaves are on the same level and every non leaf node has two children. • A complete binary tree: is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible.

  33. Heaps • A heap is a binary tree that satisfies special properties: • shape: a complete binary tree. • order: the value stored in each node is greater than or equal to the value in each of its children. • Main applications: • heap sort • to implement a priority queue

  34. treePtr 50 C 30 20 A T 10 18 Are These Heaps? treePtr

  35. 70 Is This a Heap? treePtr 12 60 40 30 8 10 Where is the largest element in a heap?

  36. 70 0 Number the Nodes Left to Right by Level tree 12 2 60 1 40 3 30 4 8 5

  37. tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 6 70 0 12 2 60 1 40 3 30 4 6 5 Use Node Numbers as Array Indexes tree.nodes

  38. Heap Sort for an Array • Make an unsorted array into a heap • Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements • Reheap the remaining unsorted elements • Repeat until all elements are sorted

  39. Reheap Down (Step 3) • To make heaps when all but the top element is in heap position • How: • swap root with left or right child, which ever violates heapness • reheap (down) with that child • Used when application wants to remove the highest element: • remove the root • put last element in root position • reheap down to restore the order

  40. 70 0 30 4 10 6 Create the Original Heap (how later) values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 6 10 12 2 60 1 40 3 6 5

  41. 70 0 30 4 10 6 Swap Root Element int last Place in Unsorted Array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 6 10 12 2 60 1 40 3 6 5

  42. 10 0 30 4 After Swapping Root Element values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 60 12 40 30 6 70 12 2 60 1 40 3 6 5 70 6 This is in the right slot No need to consider again

  43. 10 0 30 4 The Unsorted Array Is No Longer A Heap values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 10 60 12 40 30 6 12 2 60 1 40 3 6 5 10 is out of place, swap with its largest child 60 10 is out of place, swap with its largest child 40

  44. Remaining Unsorted Elements Reheaped: 60 0 30 4 values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 6 70 12 2 40 1 10 3 6 5 70 6

  45. 60 0 30 4 Swap Root Element Into Last Unsorted Place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 6 70 12 2 40 1 10 3 6 5 70 6

  46. 6 0 30 4 After Swapping Root Element values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 40 12 10 30 60 70 12 2 40 1 10 3 60 5 70 6 In the right slots No need to consider again

  47. 40 0 6 4 Reheap Remaining Unsorted Elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6

  48. 40 0 6 4 Swap Root Element Into Last Unsorted Place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6

  49. 6 0 After Swapping Root Element values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 30 12 10 40 60 70 30 1 12 2 10 3 60 5 70 6 40 4 In the right slots No need to consider again

  50. 30 0 Reheap Remaining Unsorted Elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 30 10 12 6 40 60 70 10 1 12 2 6 3 60 5 70 6 40 4

More Related