Tree

8,002
-1

Published on

Data Structures

Published in: Technology, Business
3 Comments
20 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,002
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
3
Likes
20
Embeds 0
No embeds

No notes for slide
  • Tree

    1. 1. T R E E A Lecture by Abdul Ghaffar Khan
    2. 2. Contents <ul><li>Trees. </li></ul><ul><li>Terminology. </li></ul><ul><li>Traversal of Binary Trees. </li></ul><ul><li>Breadth first and Depth first traversal </li></ul><ul><li>Expression Trees. </li></ul><ul><li>Binary Search Trees. </li></ul><ul><li>AVL Tree </li></ul>
    3. 3. Trees (Examples) <ul><li>Family Trees. </li></ul><ul><li>Organization Structure Charts. </li></ul><ul><li>Program Design. </li></ul><ul><li>Structure of a chapter in a book. </li></ul>
    4. 4. Tree ADT <ul><li>Tree definition </li></ul><ul><ul><li>A tree is a set of nodes. </li></ul></ul><ul><ul><li>The set may be empty </li></ul></ul><ul><ul><li>If not empty, then there is a distinguished node r , called root and zero or more non-empty subtrees T 1 , T 2 , … T k , each of whose roots are connected by a directed edge from r. </li></ul></ul><ul><li>Basic Terminology </li></ul><ul><ul><li>Root of a subtree is a child of r. R is the parent . </li></ul></ul><ul><ul><li>All children of a given node are called siblings . </li></ul></ul><ul><ul><li>A leaf (or external) node has no children. </li></ul></ul><ul><ul><li>An internal node is a node with one or more children </li></ul></ul>
    5. 5. More Tree Terminology <ul><li>A path from node V 1 to node V k is a sequence of nodes such that Vi is the parent of Vi+1 for 1  i  k. </li></ul><ul><li>The length of this path is the number of edges encountered. The length of the path is one less than the number of nodes on the path ( k – 1 in this example) </li></ul><ul><li>The depth of any node in a tree is the length of the path from root to the node. </li></ul><ul><li>All nodes of the same depth are at the same level . </li></ul><ul><li>The depth of a tree is the depth of its deepest leaf. </li></ul><ul><li>The height of any node in a tree is the length of the longest path from the node to a leaf. </li></ul><ul><li>The height of a tree is the height of its root. </li></ul><ul><li>If there is a path from V 1 to V 2 , then V 1 is an ancestor of V 2 and V 2 is a descendent of V 1 . </li></ul>
    6. 6. Parts of a Tree nodes
    7. 7. Parts of a Tree parent node
    8. 8. Parts of a Tree child nodes parent node
    9. 9. Parts of a Tree child nodes parent node
    10. 10. Parts of a Tree root node leaf nodes
    11. 11. Parts of a Tree sub-tree
    12. 12. Parts of a Tree sub-tree
    13. 13. Parts of a Tree sub-tree
    14. 14. Binary Tree <ul><li>Each node can have at most 2 children </li></ul>
    15. 15. Traversal <ul><li>Systematic way of visiting all the nodes. </li></ul><ul><li>Methods: </li></ul><ul><ul><li>Preorder, Inorder, and Postorder </li></ul></ul><ul><li>They all traverse the left subtree before the right subtree. </li></ul><ul><li>The name of the traversal method depends on when the node is visited. </li></ul>
    16. 16. Preorder Traversal <ul><li>Visit the node. </li></ul><ul><li>Traverse the left subtree. </li></ul><ul><li>Traverse the right subtree. </li></ul>
    17. 17. Example: Preorder 31 43 64 20 40 56 28 33 47 59 89 43 31 20 28 40 33 64 56 47 59 89
    18. 18. Inorder Traversal <ul><li>Traverse the left subtree. </li></ul><ul><li>Visit the node. </li></ul><ul><li>Traverse the right subtree. </li></ul>
    19. 19. Example: Inorder 31 43 64 20 40 56 28 33 47 59 89 20 31 28 40 33 43 56 47 59 64 89
    20. 20. Postorder Traversal <ul><li>Traverse the left subtree. </li></ul><ul><li>Traverse the right subtree. </li></ul><ul><li>Visit the node. </li></ul>
    21. 21. Example: Postorder 31 43 64 20 40 56 28 33 47 59 89 20 28 40 31 33 56 47 59 64 89 43
    22. 22. Expression Tree <ul><li>A Binary Tree built with operands and operators. </li></ul><ul><li>Also known as a parse tree. </li></ul><ul><li>Used in compilers. </li></ul>
    23. 23. Example: Expression Tree / + / 1 3 * 6 7 4 1/3 + 6*7 / 4
    24. 24. Notation <ul><li>Preorder </li></ul><ul><ul><li>Prefix Notation </li></ul></ul><ul><li>Inorder </li></ul><ul><ul><li>Infix Notation </li></ul></ul><ul><li>Postorder </li></ul><ul><ul><li>Postfix Notation </li></ul></ul>
    25. 25. Example: Infix / + / 1 3 * 6 7 4 1 / 3 + * 6 7 / 4
    26. 26. Example: Postfix 7 / 1 + / 3 * 6 4 Recall: Reverse Polish Notation / / 1 1 3 3 6 6 7 7 * * 4 4 / / + +
    27. 27. Example: Prefix / + / 1 3 * 6 7 4 + / 1 3 / * 6 7 4
    28. 28. Breadth first first traversal 1, 2, 3, 4, 5, 6, 7, 8, 9 1 2 3 4 5 6 7 8 9
    29. 29. Depth first first traversal 1, 2, 5, 6, 3, 7, 4, 8, 9 1 2 3 4 5 6 7 8 9
    30. 30. Binary Search Tree <ul><li>A Binary Tree such that: </li></ul><ul><li>Every node entry has a unique key. </li></ul><ul><li>All the keys in the left subtree of a node are less than the key of the node. </li></ul><ul><li>All the keys in the right subtree of a node are greater than the key of the node. </li></ul>
    31. 31. Example 1: 31 43 64 20 40 56 28 33 47 59 89 key is an integer
    32. 32. Insert <ul><li>Create new node for the item. </li></ul><ul><li>Find a parent node. </li></ul><ul><li>Attach new node as a leaf. </li></ul>
    33. 33. Insert 31 43 64 20 40 56 28 33 47 59 89 Example: 57
    34. 34. Insert 31 43 64 20 40 56 28 33 47 59 89 Example: 57 57
    35. 35. Search: Checklist <ul><li>if target key is less than current node’s key, search the left sub-tree . </li></ul><ul><li>else, if target key is greater than current node’s key, search the right sub-tree. </li></ul><ul><li>returns: </li></ul><ul><ul><li>if found, pointer to node containing target key. </li></ul></ul><ul><ul><li>otherwise, NULL pointer. </li></ul></ul>
    36. 36. Search 31 43 64 20 40 56 28 33 47 59 89 Example: 59 57 found
    37. 37. Search 31 43 64 20 40 56 28 33 47 59 89 Example: 61 57 failed
    38. 38. 4.4 AVL Trees <ul><li>For some binary search tree, the height could be quite large, which is not good for searching. </li></ul><ul><li>An AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance condition </li></ul><ul><ul><li>To ensure that the height of the tree is O(logN) </li></ul></ul><ul><ul><li>For every node in the AVL tree, the height of left subtree and the height of the right subtree can differ by at most one . </li></ul></ul>
    39. 39. 4.4 AVL Trees <ul><li>The height of the left subtree minus the height of the right subtree of a node is called the balance of the node . For an AVL tree, the balances of the nodes are always -1, 0 or 1 . </li></ul><ul><ul><li>The height of an empty tree is defined to be -1 . </li></ul></ul><ul><li>Given an AVL tree, if insertions or deletions are performed, the AVL tree may not remain height balanced. </li></ul>
    40. 40. 4.4 AVL Trees 5 2 8 1 4 3 7 An AVL Tree 7 2 8 1 4 3 5 Not an AVL Tree
    41. 41. 4.4 AVL Trees <ul><li>To maintain the height balanced property of the AVL tree after insertion or deletion, it is necessary to perform a transformation on the tree so that </li></ul><ul><li>(1) the inorder traversal of the transformed tree is the same as for the original tree (i.e., the new tree remains a binary search tree). </li></ul><ul><li>(2) the tree after transformation is height balanced. </li></ul>
    42. 42. <ul><li>After an insertion, only nodes that are on the path from the insertion point to the root might have their balance altered </li></ul><ul><li>Follow the path up to the root, find the first node (i.e., deepest) whose new balance violates the AVL condition. Call this node a . </li></ul><ul><li>Rebalance the tree at node a . </li></ul><ul><li>This guarantees that the entire tree is balanced . </li></ul>4.4 AVL Trees
    43. 43. 4.4 AVL Trees <ul><li>Violation may occur when an insertion into </li></ul><ul><li>1. left subtree of left child of a (LL case) </li></ul><ul><li>2. right subtree of left child of a (LR case) </li></ul><ul><li>3. left subtree of right child of a (RL case) </li></ul><ul><li>4. right subtree of right child of a (RR case) </li></ul>
    44. 44. 4.4 AVL Trees <ul><li>Rotation </li></ul><ul><li>- to restore the AVL tree after insertion </li></ul><ul><li>- single rotation </li></ul><ul><ul><ul><li>For case 1 and 4 </li></ul></ul></ul><ul><li>- double rotation </li></ul><ul><ul><ul><li>For case 2 and 3 </li></ul></ul></ul>
    45. 45. 4.4.1 AVL Trees: Single Rotation <ul><li>Example: 3 2 1 4 5 6 7 </li></ul><ul><li>construct binary search tree without height balanced restriction </li></ul><ul><li>depth of tree = 4 </li></ul>i.e. LL case
    46. 46. 4.4.1 AVL Trees: Single Rotation <ul><li>Construct AVL tree (height balanced) </li></ul>3 2 1 4 5 6 7
    47. 47. 4.4.1 AVL Trees: Single Rotation <ul><li>Insert 4, 5 </li></ul>Insert 6 3 2 1 4 5 6 7
    48. 48. 4.4.1 AVL Trees: Single Rotation <ul><li>Insert 7 </li></ul>3 2 1 4 5 6 7
    49. 49. 4.4.2 AVL Trees: Double Rotation <ul><li>Single rotation fails to fix cases 2 and 3 (i.e. LR and RL cases) </li></ul>
    50. 50. 4.4.2 AVL Trees: Double Rotation <ul><li>Double rotation is used </li></ul><ul><li>case 2 (LR case) </li></ul>
    51. 51. 4.4.2 AVL Trees: Double Rotation <ul><li>case 3 (RL case) </li></ul>
    52. 52. 4.4.2 AVL Trees: Double Rotation <ul><li>Example: </li></ul>
    53. 53. 4.4.2 AVL Trees: Double Rotation <ul><li>Insert 14 </li></ul>
    54. 54. 4.4.2 AVL Trees: Double Rotation <ul><li>Insert 13 </li></ul>
    55. 55. 4.4.2 AVL Trees: Double Rotation <ul><li>Insert 12 </li></ul>
    56. 56. 4.4.2 AVL Trees: Double Rotation <ul><li>Insert 11 and 10 (single rotation), then 8 </li></ul>
    57. 57. 4.4.2 AVL Trees: Double Rotation <ul><li>Insert 9 </li></ul>

    ×