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
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
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
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
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
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
? ? ? ? ? ? BinaryTree ADT Operations ? ? ? ? ? ? ? � 66
#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_
#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_
Considerations � 69
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
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
A Different Approach 3 4 2 0 -1 -2 6 � 72
A Different Approach 3 4 2 0 -1 -2 6 � 73
A Different Approach 2 3 4 0 -1 -2 6 � 74
A Different Approach 2 -1 4 3 0 -2 6 � 75
A Different Approach 2 -1 4 3 0 -2 6 � 76
A Different Approach 2 -1 4 3 0 -2 6 � 77
A Different Approach 2 -1 4 3 -2 0 6 � 78
A Different Approach 2 -1 4 3 -2 0 6 � 79
A Different Approach 2 -1 4 3 0 -2 6 � 80
A Different Approach 2 -1 4 3 0 -2 6 � 81
A Different Approach 2 -1 4 0 6 -2 3 � 82
A Different Approach 2 -1 4 0 6 -2 3 � 83
A Different Approach 2 -1 4 0 6 -2 3 � 84
A Different Approach Find 5 2 -1 4 0 6 -2 3 � 85
A Different Approach Find 5 2 -1 4 0 6 -2 3 � 86
A Different Approach Find 5 2 -1 4 0 6 -2 3 � 87
A Different Approach Find 5 2 -1 4 0 6 -2 3 � 88
A Different Approach What’s special about the shape of 2 this tree? -1 4 0 6 -2 3 � 89
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
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
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
Inserting into a BST 2 -1 4 0 6 -2 3 1 � 93
Inserting into a BST 2 -1 4 0 6 -2 3 1 � 94
Inserting into a BST 2 -1 4 0 6 -2 3 1 � 95
Inserting into a BST 2 -1 4 0 6 -2 3 1 � 96
Inserting into a BST 2 -1 4 0 6 -2 3 1 � 97
Inserting into a BST 2 -1 4 0 6 -2 3 1 � 98
Inserting into a BST 2 -1 4 0 6 -2 3 1 5 � 99
Inserting into a BST 2 -1 4 0 6 -2 3 1 5 � 100
Recommend
More recommend