trees
play

Trees Tiziana Ligorio 1 Todays Plan Trees Binary Tree ADT - PowerPoint PPT Presentation

Trees Tiziana Ligorio 1 Todays Plan Trees Binary Tree ADT Binary Search Tree ADT 2 Announcements Questions? 3 ADT Operations we have seen so far Bag, List, Stack, Queue Add data to collection Remove data from


  1. Visit (retrieve, print, modify …) every node in the tree 
 Preorder Traversal: if (T is not empty) //implicit base case 
 { 
 visit the root r 
 traverse T L 
 traverse T R 
 } 
 60 r 20 70 T R T L 40 10 50 30 Preorder: 60, 20, 10, 40, 30, 50 � 60

  2. Visit (retrieve, print, modify …) every node in the tree 
 Preorder Traversal: if (T is not empty) //implicit base case 
 { 
 visit the root r 
 traverse T L 
 traverse T R 
 } 
 60 r 20 70 T R T L 40 10 50 30 Preorder: 60, 20, 10, 40, 30, 50, 70 � 61

  3. Visit (retrieve, print, modify …) every node in the tree 
 Preorder Traversal: if (T is not empty) //implicit base case 
 { 
 visit the root r 
 traverse T L 
 traverse T R 
 } 
 60 r 20 70 T R T L 40 10 50 30 Preorder: 60, 20, 10, 40, 30, 50, 70 � 62

  4. Visit (retrieve, print, modify …) every node in the tree 
 Preorder Traversal: if (T is not empty) //implicit base case 
 { 
 visit the root r 
 traverse T L 
 traverse T R 
 } 
 60 r 20 70 T R T L 40 10 50 30 Preorder: 60, 20, 10, 40, 30, 50, 70 � 63

  5. Visit (retrieve, print, modify …) every node in the tree 
 Inorder Traversal: if (T is not empty) //implicit base case 
 { 
 traverse T L 
 visit the root r 
 traverse T R 
 } 
 60 r 20 70 T R T L 40 10 50 30 Inorder: 10, 20, 30, 40, 50, 60, 70 � 64

  6. 
 Visit (retrieve, print, modify …) every node in the tree 
 Postorder Traversal: if (T is not empty) //implicit base case 
 { 
 traverse T L 
 traverse T R visit the root r 
 } 
 60 r 20 70 T R T L 40 10 50 30 Postorder: 10, 30, 50, 40, 20, 70, 60 � 65

  7. ? ? ? ? ? ? BinaryTree ADT Operations ? ? ? ? ? ? ? � 66

  8. #ifndef BinaryTree_H_ 
 #define BinaryTree_H_ 
 template<typename ItemType> 
 class BinaryTree 
 { 
 public: 
 BinaryTree(); // constructor 
 BinaryTree(const BinaryTree<ItemType>& tree); // copy constructor 
 ~BinaryTree(); // destructor 
 bool isEmpty() const; 
 size_t getHeight() const; 
 size_t getNumberOfNodes() const; 
 void add(const ItemType& new_item); 
 void remove(const ItemType& new_item); 
 ItemType find(const ItemType& item) const; 
 void clear(); 
 void preorderTraverse(Visitor<ItemType>& visit) const; 
 void inorderTraverse(Visitor<ItemType>& visit) const; 
 void postorderTraverse(Visitor<ItemType>& visit) const; 
 BinaryTree& operator= (const BinaryTree<ItemType>& rhs); 
 private: // implementation details here 
 };// end BST #include "BinaryTree.cpp" 
 � 67 #endif // BinaryTree_H_

  9. #ifndef BinaryTree_H_ 
 #define BinaryTree_H_ 
 template<typename ItemType> 
 class BinaryTree 
 { 
 public: 
 BinaryTree(); // constructor 
 BinaryTree(const BinaryTree<ItemType>& tree); // copy constructor 
 ~BinaryTree(); // destructor 
 bool isEmpty() const; 
 size_t getHeight() const; 
 size_t getNumberOfNodes() const; 
 How might you add void add(const ItemType& new_item); 
 Will determine the tree structure void remove(const ItemType& new_item); 
 ItemType find(const ItemType& item) const; 
 void clear(); 
 void preorderTraverse(Visitor<ItemType>& visit) const; 
 void inorderTraverse(Visitor<ItemType>& visit) const; 
 void postorderTraverse(Visitor<ItemType>& visit) const; 
 BinaryTree& operator= (const BinaryTree<ItemType>& rhs); 
 private: // implementation details here 
 This is an abstract class from which };// end BST we can derive desired behavior #include "BinaryTree.cpp" 
 keeping the traversal general � 68 #endif // BinaryTree_H_

  10. Considerations � 69

  11. Recall Remember our Bag ADT? 
 - Array implementation 
 - Linked Chain implementation 
 - Assume no duplicates Find an element: O(n) Remove: Find element and if there remove it O(n) Add: Check if element is there and if not add it O(n) � 70

  12. Recall Remember our Bag ADT? 
 Can we do better? - Array implementation 
 - Linked Chain implementation 
 - Assume no duplicates Find an element: O(n) Remove: Find element and if there remove it O(n) Add: Check if element is there and if not add it O(n) � 71

  13. A Different Approach 3 4 2 0 -1 -2 6 � 72

  14. A Different Approach 3 4 2 0 -1 -2 6 � 73

  15. A Different Approach 2 3 4 0 -1 -2 6 � 74

  16. A Different Approach 2 -1 4 3 0 -2 6 � 75

  17. A Different Approach 2 -1 4 3 0 -2 6 � 76

  18. A Different Approach 2 -1 4 3 0 -2 6 � 77

  19. A Different Approach 2 -1 4 3 -2 0 6 � 78

  20. A Different Approach 2 -1 4 3 -2 0 6 � 79

  21. A Different Approach 2 -1 4 3 0 -2 6 � 80

  22. A Different Approach 2 -1 4 3 0 -2 6 � 81

  23. A Different Approach 2 -1 4 0 6 -2 3 � 82

  24. A Different Approach 2 -1 4 0 6 -2 3 � 83

  25. A Different Approach 2 -1 4 0 6 -2 3 � 84

  26. A Different Approach Find 5 2 -1 4 0 6 -2 3 � 85

  27. A Different Approach Find 5 2 -1 4 0 6 -2 3 � 86

  28. A Different Approach Find 5 2 -1 4 0 6 -2 3 � 87

  29. A Different Approach Find 5 2 -1 4 0 6 -2 3 � 88

  30. A Different Approach What’s special about the shape of 2 this tree? -1 4 0 6 -2 3 � 89

  31. Binary Search Tree Structural Property: For each node n 
 n > all values in T L 2 n < all values in T R -1 4 0 6 -2 3 � 90

  32. BST Formally Let S be a set of values upon which a total ordering relation <, is defined. For example, S can be the set of integers. 
 A binary search tree ( BST ) T for the ordered set (S,<) is a binary tree with the following properties: • Each node of T has a value. If p and q are nodes, then we write p < q to mean that the value of p is less than the value of q. 
 • For each node n ∈ T, if p is a node in the left subtree of n, then p < n. 
 • For each node n ∈ T, if p is a node in the right subtree of n, then n < p. 
 • For each element s ∈ S there exists a node n ∈ T such that s = n. � 91

  33. Binary Search Tree Structural Property: For each node n 
 n > all values in T L n < all values in T R r T L T R 
 search (bs_tree, item) 
 < r > r { if (bs_tree is empty) //base case 
 item not found 
 else if (item == root) 
 return root 
 else if (item < root) 
 search (T L , item) 
 else // item > root 
 search (T R , item) 
 } 
 � 92

  34. Inserting into a BST 2 -1 4 0 6 -2 3 1 � 93

  35. Inserting into a BST 2 -1 4 0 6 -2 3 1 � 94

  36. Inserting into a BST 2 -1 4 0 6 -2 3 1 � 95

  37. Inserting into a BST 2 -1 4 0 6 -2 3 1 � 96

  38. Inserting into a BST 2 -1 4 0 6 -2 3 1 � 97

  39. Inserting into a BST 2 -1 4 0 6 -2 3 1 � 98

  40. Inserting into a BST 2 -1 4 0 6 -2 3 1 5 � 99

  41. Inserting into a BST 2 -1 4 0 6 -2 3 1 5 � 100

Recommend


More recommend