week 8
play

Week 8 Oliver Kullmann Binary trees The notion BinaryTrees of - PowerPoint PPT Presentation

CS 270 Algorithms Week 8 Oliver Kullmann Binary trees The notion BinaryTrees of binary search tree Tree traversal Binary trees 1 Queries in binary The notion of binary search tree search trees 2 Insertion Tree


  1. CS 270 Algorithms Week 8 Oliver Kullmann Binary trees The notion BinaryTrees of “binary search tree” Tree traversal Binary trees 1 Queries in binary The notion of “binary search tree” search trees 2 Insertion Tree traversal Deletion 3 Tutorial Queries in binary search trees 4 Insertion 5 Deletion 6 Tutorial 7

  2. CS 270 General remarks Algorithms Oliver Kullmann Binary trees The notion of “binary search tree” We consider binary search trees , another important data Tree structure. traversal Queries in We learn how to use them, by making efficient queries. binary search trees And we learn how to build them. Insertion Deletion Reading from CLRS for week 8 Tutorial 1 Sections 12.1, 12.2, 12.3 on binary search trees.

  3. CS 270 Recall: Binary trees Algorithms Oliver Kullmann Binary trees The notion Last week we introduced the following hierarchy of concepts: of “binary search tree” Tree trees acyclic connected graphs with at least one vertex traversal Queries in rooted trees trees with a distinguished node as root binary search trees ordered trees rooted trees, where the children of each node are Insertion in a linear order Deletion binary trees ordered trees, where every node has at most two Tutorial children, and where each child is designated as “left” or “right” child.

  4. CS 270 The number of nodes in binary trees Algorithms Oliver Kullmann For a binary tree T we have the following bounds on the Binary trees number #nds( T ) of nodes in T : The notion of “binary search tree” 1 + ht( T ) ≤ #nds( T ) ≤ 2 ht( T )+1 − 1 Tree traversal (recall that ht( T ) is the height of T ). Queries in binary search trees The lower bound is attained iff T is degenerated , that is, is Insertion a (single) path. Deletion The upper bound is attained iff T is a perfect binary tree Tutorial (that is, all paths to leaves have the same length). So it is possible to put an exponential amount of information into a tree of a given height, and this is what one wants when using trees for data storage.

  5. CS 270 Examples Algorithms Oliver Kullmann The perfect binary tree Binary trees The notion 1 of “binary ❘ 2 ❧❧❧❧❧❧❧❧❧❧❧❧❧ ❘ ❘ ❘ search tree” ❘ ❘ ❘ ❘ ❘ ❘ ❘ Tree ❘ ❘ 3 traversal ❑ 4 sssssssss ✾ ❑ ❑ ✾ ✆✆✆✆ ❑ Queries in ❑ ✾ ❑ ❑ ✾ binary ❑ ❑ search trees 5 6 7 ✾ ✾ ✾ ✾ ✾ ✾ ✾ ✾ Insertion ✆✆✆✆ ✆✆✆✆ ✆✆✆✆ ✆✆✆✆ ✾ ✾ ✾ ✾ ✾ ✾ ✾ ✾ Deletion 8 9 10 11 12 13 14 15 Tutorial has height 3 and 2 3+1 − 1 = 15 nodes (and 2 3 = 8 leaves). A smallest binary tree of height 3, i.e., a degenerated tree of height 3, would consist of exactly one path from the root to some leaf in that tree, with 3 + 1 = 4 nodes.

  6. CS 270 Balanced binary trees Algorithms Oliver Kullmann Binary trees Since for a data structure one wants to store as much The notion of “binary information as possible into a binary tree for a given height , search tree” perfect binary trees are “perfect”. Tree traversal However that notion is very stiff, and a relaxation of this notion Queries in binary is the notion of a “balanced binary tree”. search trees Insertion Deletion We do not go deeper into the discussion of “balancedness”, Tutorial and for us a “balanced binary tree” T is one where the height ht( T ) is “close” to lg(#nds( T )) (close to the binary logarithm of the number of nodes).

  7. CS 270 Overview on binary search trees Algorithms Oliver Binary search trees are binary trees used for storing data: Kullmann Binary trees All operations for dynamic sets can be implemented. The notion All these operations have running time linear in the height of “binary search tree” of the (current) binary search tree. Tree traversal Thus it is crucial to ensure that the binary search trees Queries in binary constructed have low height (are close to be balanced): search trees Insertion Deletion In the best case all operations need logarithmic time Tutorial (e.g., when the tree is perfect) — but in the worst case all need linear time (e.g., when the tree is degenerated). How to ensure that we are close to the best case is a more complicated topic, which is not covered in this module (see CLRS for more information).

  8. CS 270 The binary-search-tree property Algorithms Oliver Kullmann A binary search tree is a binary tree T , where we have a linear Binary trees order on the universe of keys, such that The notion of “binary if w is a node of T with key k , search tree” then for every key k ′ in the left subtree of T (if it exists) Tree traversal we have Queries in k ′ ≤ k binary search trees while for every key k ′′ in the right subtree of T (if it exists) Insertion Deletion we have Tutorial k ≤ k ′′ . In other words: Going left the keys get smaller, going right the keys get larger.

  9. CS 270 Examples Algorithms Oliver Examples for binary search trees (with integers as keys) are Kullmann 5 ❖ Binary trees 3 ♦♦♦♦♦♦♦♦♦♦♦ ❖ ❖ ❖ ❖ ❖ ❖ The notion ❖ ❖ ❖ ❖ of “binary 7 search tree” ❄ ❄ ❄ ❄ ⑧ ⑧ ⑧ ❄ ⑧ ❄ Tree ⑧ ❄ ⑧ ❄ ❄ ❄ ⑧ ⑧ traversal ⑧ ⑧ 2 4 6 9 Queries in binary search trees and 1 Insertion ❄ ❄ ❄ ❄ Deletion ❄ 2 Tutorial ❄ ❄ ❄ ❄ ❄ 3 Remark: Both trees have height 2 — obviously the first one contains (“much”) more information (is better). Question: If we want to add a new leaf with key 5, where can this be done in both trees?

  10. CS 270 Further examples Algorithms Oliver Kullmann Still an example is 5 Binary trees ❖ 3 ♦♦♦♦♦♦♦♦♦♦♦ ❖ ❖ ❖ ❖ ❖ The notion ❖ ❖ ❖ of “binary ❖ ❖ search tree” 7 ❄ ❄ ⑧ ❄ ⑧ ❄ Tree ⑧ ❄ ⑧ ❄ ❄ ❄ ⑧ ⑧ traversal ⑧ ❄ ⑧ ❄ ⑧ ⑧ 2 4 5 9 Queries in binary search trees while 5 ❖ 3 ♦♦♦♦♦♦♦♦♦♦♦ ❖ Insertion ❖ ❖ ❖ ❖ ❖ ❖ Deletion ❖ ❖ ❖ 7 Tutorial ❄ ❄ ❄ ❄ ⑧ ⑧ ⑧ ❄ ⑧ ❄ ⑧ ❄ ⑧ ❄ ❄ ❄ ⑧ ⑧ ⑧ ⑧ 2 4 4 9 is not. Question: Where are the three places adding another leaf with key 5 to the first tree? And where are the two places to add 3 ?

  11. CS 270 Walking the tree Algorithms Oliver Kullmann Our first task is for a binary search tree T to print out the keys Binary trees in T in sorted order. With a recursive procedure this is very The notion of “binary easy: search tree” Tree 1 First print out the keys of the left subtree of the root. traversal Queries in 2 Then print the key of the root. binary search trees 3 Finally print out the keys of the right subtree of the root. Insertion Deletion In other words, we just have to perform an inorder tree Tutorial traversal (or “walk”) of T . “Inorder” here means that the root is handled in-between the two subtrees. Since every node is traversed just once, obviously the running time of INORDER-TREE-WALK is linear in the number of nodes.

  12. CS 270 Pseudo-code for the inorder traversal Algorithms Oliver Using a bit more precision, the parameter of Kullmann INORDER-TREE-WALK shall best be Binary trees The notion a pointer x to some node of T , and of “binary search tree” the algorithm then traverses the subtree of T with root x . Tree traversal Calling the procedure with the root of T , we walk the whole Queries in binary tree. The procedure in pseudo-code is then as follows (recall search trees that Java-like semantics is used, and thus a pointer like x is Insertion (normally!) automatically de-referenced): Deletion Tutorial INORDER-TREE-WALK ( x ) 1 if ( x != NIL ) { 2 INORDER-TREE-WALK ( x . left ); 3 print x . key ; 4 INORDER-TREE-WALK ( x . right ); 5 }

  13. CS 270 Examples Algorithms 5 Oliver ❖ 3 ♦♦♦♦♦♦♦♦♦♦♦ ❖ Kullmann ❖ ❖ ❖ ❖ ❖ ❖ ❖ ❖ ❖ Binary trees 7 ❄ ❄ The notion ⑧ ❄ ⑧ ❄ ❄ ❄ ⑧ ⑧ of “binary ⑧ ❄ ⑧ ❄ ⑧ ❄ ⑧ ❄ search tree” ⑧ ⑧ 2 4 5 9 Tree traversal yields (only showing the keys) 2 , 3 , 4 , 5 , 5 , 7 , 9. Queries in binary Just performing the inorder traversal on a binary tree (not a search trees search-tree) Insertion a Deletion ❖ b ♦♦♦♦♦♦♦♦♦♦♦ ❖ ❖ ❖ ❖ ❖ Tutorial ❖ ❖ ❖ ❖ ❖ c ❄ ❄ ⑧ ❄ ⑧ ❄ ❄ ⑧ ❄ ⑧ ⑧ ❄ ⑧ ❄ ⑧ ⑧ ❄ ❄ ⑧ ⑧ g e d f ❄ ❄ ⑧ ❄ ⑧ ❄ ⑧ ❄ ⑧ ⑧ h i yields d, b, e, h, a, f, c, i, g.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend