1 / 47

Trees Types and Operations

Trees Types and Operations. Agenda. General Trees Binary Search Trees AVL Trees Heap Trees. 1. General Trees. Insertion FIFO LIFO Key-sequenced Insertion Deletion Changing a General Tree into a Binary Tree. Insertion. Given the parent Node a new node may be inserted as FIFO.

mahsa
Download Presentation

Trees Types and Operations

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. TreesTypes and Operations

  2. Agenda • General Trees • Binary Search Trees • AVL Trees • Heap Trees

  3. 1. General Trees • Insertion • FIFO • LIFO • Key-sequenced Insertion • Deletion • Changing a General Tree into a Binary Tree

  4. Insertion Given the parent Node a new node may be inserted as • FIFO

  5. First in-first out (FIFO) insertion Data Structures: A Pseudocode Approach with C

  6. Insertion Given the parent Node a new node may be inserted as • FIFO • LIFO

  7. Last in-first out (LIFO) insertion Data Structures: A Pseudocode Approach with C

  8. Insertion Given the parent Node a new node may be inserted as • FIFO • LIFO • Key-sequenced Insertion

  9. Key-sequenced insertion Data Structures: A Pseudocode Approach with C

  10. 1. General Trees • Insertion • FIFO • LIFO • Key-sequenced Insertion • Deletion • Changing a General Tree into a Binary Tree

  11. Deletion • For general trees nodes to be deleted are restricted to be “leaves” • Otherwise a node maybe “purged”, i.e. a node is deleted along with all its children

  12. 1. General Trees • Insertion • FIFO • LIFO • Key-sequenced Insertion • Deletion • Changing a General Tree into a Binary Tree

  13. Changing into Binary Trees • Changing the meaning of the two pointers: • Leftchild …..first child • Rightchild ….. Next siblings

  14. Changing a General Tree to a Binary Tree Data Structures: A Pseudocode Approach with C

  15. Changing a General Tree to a Binary Tree Data Structures: A Pseudocode Approach with C

  16. Changing a General Tree to a Binary Tree Data Structures: A Pseudocode Approach with C

  17. Agenda • General Trees • Binary Search Trees • AVL Trees • Heap Trees

  18. 2. Binary Search Tree • Basic Concepts • BST Operations • Threaded Trees

  19. Basic Concepts • All items in left subtree < root • All items in right subtree > root

  20. Binary Search Trees A binary search tree Not a binary search tree

  21. Binary Search Tree Two binary search trees representing the same set:

  22. 2. Binary Search Tree • Basic Concepts • BST Operations • Threaded Trees

  23. BST Operations • Traversal • Search • Smallest ……….. ? • Largest …………? • Specific element • Insertion • Deletion

  24. Inorder traversal of BST • Print out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

  25. BST Operations • Traversal • Search • Smallest ……….. ? • Largest …………? • Specific element • Insertion • Deletion

  26. findMin/ findMax • Return the node containing the smallest element in the tree • Start at the root and goes left/right as long as there is a left/right child. The stopping point is the smallest/largest element • Time complexity = O(height of the tree)

  27. Searching BST (specific elem) • If we are searching for 15, then we are done. • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree.

  28. Searching BST

  29. BST Operations • Traversal • Search • Smallest ……….. ? • Largest …………? • Specific element • Insertion • Deletion

  30. insert • Proceed down the tree as you would with a find • If X is found, do nothing (or update something) • Otherwise, insert X at the last spot on the path traversed • Time complexity = O(height of the tree)

  31. BST Operations • Traversal • Search • Smallest ……….. ? • Largest …………? • Specific element • Insertion • Deletion

  32. delete • When we delete a node, we need to consider how we take care of the children of the deleted node. • This has to be done such that the property of the search tree is maintained.

  33. delete Three cases: (1) the node is a leaf • Delete it immediately (2) the node has one sub-tree (right or left) • Adjust a pointer from the parent to bypass that node

  34. delete (3) the node has 2 children • replace the key of that node with the minimum element at the right subtree(or the maximum element at the left subtree) • delete the minimum element • Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. • Time complexity = O(height of the tree)

  35. delete

  36. 2. Binary Search Tree • Basic Concepts • BST Operations • Threaded Trees

  37. Threaded Trees • Sparing recursion and stack • Making use of null right child of leaves to point to next node

  38. Agenda • General Trees • Binary Search Trees • AVL Trees • Heap Trees

  39. 3. AVL Trees • Properties • Operations

  40. Properties of AVL Trees • It is a balanced binary tree(definition of Russian mathematicians Adelson-VelskiiandLandis) • The height of its sub-trees differs by no more than one (its balance factor is -1, 0, or 1), and its subtrees are also balanced.

  41. Properties of AVL Trees • A sub tree is called • Left high (LH) if its balance is 1 • Equally high (EH) if it is 0 • Right high (RH) if it is -1

  42. Operations on AVL Trees • Insertion and deletion are same as in BST • If unbalance occurs corresponding rotations must be performed to restore balance

  43. Balanced trees: AVL tree rotations • Steps: • Check if case is case 1 or 2 of the following and act accordingly • Case 1: • tree is left high & • out-of-balance is created by a adding node to the left of the left sub-tree • …… One right rotation is needed Rotate out-of-balance node right

  44. Case 1: single R-rotation • Case 1 * • Tree is left balanced • unbalance is caused by node on the left of left sub-tree h+2 h+1 h+1 h+1 h h h h h

  45. Balanced trees: AVL tree rotations • Case 2: • tree is left high • out-of-balance is created by a adding node to the right of the left sub-tree • …… Two rotations are needed: Move from bottom of left sub-tree upwards till an unbalanced node is found and rotate it left Rotate left sub-tree right

  46. Case 2: Double LR-rotation Add node to right of left balanced subtree h+2 h+1 h+2 h+2 h h h+1 h+1 h+1 h h h First rotation .. Left rotation of unbalanced node c Second rotation … Right rotation of left sub-tree g

  47. End

More Related