1 / 81

Analysis & Design of Algorithms (CSCE 321)

Analysis & Design of Algorithms (CSCE 321). Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees. Dictionaries(2): Binary Search Trees. Binary Trees Tree Traversal The Binary Search Tree (BST) Deleting Nodes from a BST Binary Search Tree ADT

Download Presentation

Analysis & Design of Algorithms (CSCE 321)

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. Analysis & Design of Algorithms(CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees Prof. Amr Goneid, AUC

  2. Dictionaries(2): Binary Search Trees • Binary Trees • Tree Traversal • The Binary Search Tree (BST) • Deleting Nodes from a BST • Binary Search Tree ADT • Template Class Specification • Other Search Trees Prof. Amr Goneid, AUC

  3. 1. Binary Trees • A Tree is a non-linear, hierarchical one-to-many data structure. Examples: Binary Trees, Binary Search Trees (BST) • Can be implemented using arrays, structs and pointers • Used in problems dealing with: • Searching • Hierarchy • Ancestor/descendant relationship • Classification Prof. Amr Goneid, AUC

  4. a a b Tree Terminology • A tree consists of a finite set of nodes (or vertices) and a finite set of edges that connect pairs of nodes. • A node is a container for data • An edge represents a relationship between two nodes. a node an edge Prof. Amr Goneid, AUC

  5. d a g b e c f Tree Terminology • A non-empty tree has a root node (e.g.node a). Every other node can be reached from the root via a unique path (e.g. a-d-f). a is the parent of nodes b and c because there is an edge going from a down to each. Moreover, b and c are children of a. root parent of g child of a Prof. Amr Goneid, AUC

  6. c a e b d Tree Terminology • The descendants of a are (b, c, d, and e), nodes that can be reached by paths from a. • The ancestors of e are (c and a), nodes found on the path from e to a. • Nodes (b, d, and e) are leaf nodes (they have no children). • Each of the nodes a and c has at least one child and is an internal node. Siblings Prof. Amr Goneid, AUC

  7. a b d e c f g Tree Terminology • Each node in the tree (except leaves) may have one or more subtrees • For a tree with n nodes there are n-1 edges Prof. Amr Goneid, AUC

  8. a b d e c f The Binary Tree Level 1 A binary tree of height h = 3 • A binary tree is a tree in which a parent has at most two children. It consists of a root and two disjoint subtrees (left and right). • The root is at level (L = 1) and the height h = maximum level • The maximum number of nodes in level L is 2L-1 Left Right Level 2 Level 3 Prof. Amr Goneid, AUC

  9. a b d e c f g The Full Binary Tree • A binary tree is full iff the number of nodes in level L is 2L-1 Prof. Amr Goneid, AUC

  10. The Full Binary Tree • A full binary tree of height h has n nodes, where Prof. Amr Goneid, AUC

  11. The Full Binary Tree Prof. Amr Goneid, AUC

  12. The Full Binary Tree • The cost of search for nodes in level L is L 2L-1 • Hence, the total search cost is Prof. Amr Goneid, AUC

  13. The Balanced Tree • A balanced binary tree has the property that the heights of the left and right subtrees differ at most by one level. • i.e. |hL – hR| ≤ 1 • A Full tree is also a balanced tree hL hR Prof. Amr Goneid, AUC

  14. Complete Binary Tree • A binary tree is Complete iff the number of Nodes at level 1 <= L <= h-1 is 2L-1 and leaf nodes at level h occupy the leftmost positions in the tree • i.e. all levels are filled except the rightmost of the last level. Missing Leaves Missing Leaves Prof. Amr Goneid, AUC

  15. Complete Binary Tree A complete binary tree can be efficiently implemented as an array, where a node at index i has children at indexes 2i and 2i+1 and a parent at index i/2 (with one-based indexing). 1 A 2 3 B C 4 5 D E Prof. Amr Goneid, AUC

  16. Binary Tree as a Recursive Structure • A binary tree is a recursive structure • e.g. it can be defined recursively as: if (not empty tree) 1. It has a root 2. It has a (Left Subtree) 3. It has a (Right Subtree) • Recursive structure suggests recursive processing of trees (e.g. Traversal) a b c f d e Prof. Amr Goneid, AUC

  17. Binary Tree as a Recursive Structure Empty a,b,c,d,e,f a b,d,e c,f b c f d e Prof. Amr Goneid, AUC

  18. 2. Tree Traversal • Traversal is to visit every node ( to display, process, …) exactly once • It can be done recursively • There are 3 different binary tree traversal orders: • Pre-Order: Root is visited before its two subtrees • In-Order: Root is visited in between its two subtrees • Post-Order: Root is visited after its two subtrees Prof. Amr Goneid, AUC

  19. Pre-Order Traversal • Algorithm: PreOrder ( tree ) { if ( not empty tree) { Visit (root); PreOrder (left subtree); PreOrder (right subtree); } } • The resulting visit order = {a} {b , d , e} {c , f } 1 a 5 2 b c 4 3 f d e 6 Prof. Amr Goneid, AUC

  20. Pre-Order Traversal • Pre-Order Traversal is also called Depth-First traversal 1 2 5 3 4 6 7 Prof. Amr Goneid, AUC

  21. In-Order Traversal • Algorithm: InOrder ( tree ) { if ( not empty tree) { InOrder (left subtree); Visit (root); InOrder (right subtree); } } • The resulting visit order = {d , b , e} {a} {f , c } 4 a 6 2 b c 3 1 f d e 5 Prof. Amr Goneid, AUC

  22. Post-Order Traversal • Algorithm: PostOrder ( tree ) { if ( not empty tree) { PostOrder (left subtree); PostOrder (right subtree); Visit (root); } } • The resulting visit order = {d , e , b} {f , c } {a} 6 a 5 3 b c 2 1 f d e 4 Prof. Amr Goneid, AUC

  23. Example: Expression Tree • The expression A – B * C + D can be represented as a tree. • In-Order traversal gives: A – B * C + D This is the infix representation • Pre-Order traversal gives: + - A * B C D This is the prefix representation • Post-Order traversal gives: A B C * - D + This is the postfix (RPN) representation + - D A * C B Prof. Amr Goneid, AUC

  24. Binary Tree Traversal Demo http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BTree.html Prof. Amr Goneid, AUC

  25. 3. Binary Search Trees BST Prof. Amr Goneid, AUC

  26. 3. The Binary Search Tree (BST) • A Binary Search Tree (BST) is a Dictionary implemented as a Binary Tree. It is a form of container that permits access by content. • It supports the following main operations: • Insert : Insert item in BST • Remove : Delete item from BST • Search : search for key in BST Prof. Amr Goneid, AUC

  27. v u w BST A BST is a binary tree that stores keys or key-data pairs in its nodes and has the following properties: • A key identifies uniquely the node (no duplicate keys) • If (u , v , w) are nodes such that (u) is any node in the left subtree of (v) and (w) is any node in the right subtree of (v) then: key(u) < key(v) < key(w) Prof. Amr Goneid, AUC

  28. Examples Of BST Prof. Amr Goneid, AUC

  29. Examples Of BST These are NOT BSTs. Prof. Amr Goneid, AUC

  30. Searching Algorithm if (tree is empty) target is not in the tree else if (the target key is the root) target found in root else if (target key smaller than the root’s key) search left sub-tree else search right sub-tree Prof. Amr Goneid, AUC

  31. Searching for a key Search for the node containing e: Maximum number of comparisons is tree height, i.e. O(h) Prof. Amr Goneid, AUC

  32. BST Demo http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BST.html Prof. Amr Goneid, AUC

  33. Building a Binary Search Tree • Tree created from root downward • Item 1 stored in root • Next item is attached to left tree if value is smaller or right tree if value is larger • To insert an item into an existing tree, we must first locate the item’s parent and then insert Prof. Amr Goneid, AUC

  34. Algorithm for Insertion if (tree is empty) insert new item as root else if (root key matches item) skip insertion (duplicate key) else if (new key is smaller than root) insert in left sub-tree else insert in right sub-tree Prof. Amr Goneid, AUC

  35. Example: Building a TreeInsert: 40,20,10,50,65,45,30 Prof. Amr Goneid, AUC

  36. Effect of Insertion Order • The shape of the tree depends on the order of insertion. Shape determines the height (h) of the tree. • Since cost of search is O(h), the insertion order will affect the search cost. • The previous tree is full, and h = log2(n+1) so that search cost is O(log2n) Prof. Amr Goneid, AUC

  37. Effect of Insertion Order • The previous tree would look like a linked list if we have inserted in the order 10,20,30,…. Its height would be h = n and its search cost would be O(n) O(log n) O(n) Prof. Amr Goneid, AUC

  38. Binary Search Tree Demo http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSTNew.html Prof. Amr Goneid, AUC

  39. Traversing a Binary Search Tree Recursive inorder traversal of tree with root (t) traverse ( t ) { if (t is not empty) traverse (tleft); visit (t); traverse (tright); } Prof. Amr Goneid, AUC

  40. Find Minimum Key Find the minimum key in a tree with root (t) Minkey ( t ) { if (tleft is not empty) return MinKey(tleft); else return key(t); } Prof. Amr Goneid, AUC

  41. Other Traversal Orders • Pre-order (a.k.a. Depth-First traversal) can be implemented using an iterative (non-recursive) algorithm. In this case, a stack is used • If the stack is replaced by a queue and left pointers are exchanged by right pointers, the algorithm becomes Level-order traversal (a.k.a. Breadth-First traversal) Prof. Amr Goneid, AUC

  42. Iterative Preorder Traversal void iterative_preorder ( ) { t = root; Let s be a stack s.push (t); while(!s.stackIsEmpty()) { s.pop(t); process(t->key); if ( t right is not empty) s.push(t right); if ( t left is not empty) s.push(t left); } } Prof. Amr Goneid, AUC

  43. Pre-Order Traversal Traversal order: {D,B,A,C,F,E,G} 1 D 2 5 B F 3 4 A C E G 6 7 Prof. Amr Goneid, AUC

  44. Level Order Traversal void levelorder ( ) { t = root; Let q be a queue; q.enqueue(t); while(!q.queueIsEmpty()) { q.dequeue(t); process(t->key); if ( t left is not empty) q.enqueue(t left); if ( t right is not empty) q.enqueue(t right); } } Prof. Amr Goneid, AUC

  45. Level-Order Traversal Traversal order: {D,B,F,A,C,E,G} 1 D 2 3 B F 4 6 7 5 A C E G Prof. Amr Goneid, AUC

  46. 4. Deleting Nodes from a BST Prof. Amr Goneid, AUC

  47. Deleting a ROOT Node Prof. Amr Goneid, AUC

  48. Deleting a ROOT Node Prof. Amr Goneid, AUC

  49. Deleting a ROOT Node (Special Case) Prof. Amr Goneid, AUC

  50. Deleting a ROOT Node (Alternative) Prof. Amr Goneid, AUC

More Related