Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees.

Similar presentations


Presentation on theme: "Binary Search Trees."— Presentation transcript:

1 Binary Search Trees

2 Why Use Trees? AKA what's wrong with linked lists?
Accessing an item in a linked list takes O(N) time in average and worst case Binary trees can improve on this – reduce access time to O(logN) in average case Expands on the idea of binary search and allows insertions and deletions Worst case degenerates to O(N) but this can be avoided by using balanced trees

3 Binary Search Trees binary search tree: a binary tree in which every node's left subtree holds values less than the node's value, and its right subtree holds values greater than the node's value The left and right subtrees of a BST are also BSTs. Elements are stored in sorted order 25 17 34

4 BST Insertion Add the following values one at a time to an initially empty BST. What does the tree look like?

5 Traversals What happens in an in-order traversal of a binary search tree?

6 Question After adding N distinct values in random order to a BST what is the expected height of the tree? A. O(N1/2) B. O(logN) C. O(N) D. O(NlogN) E. O(N2)

7 Question After adding N distinct values to a BST what is the worst case height of the tree? A. O(N1/2) B. O(logN) C. O(N) D. O(NlogN) E. O(N2)

8 Example Insert the elements below into an initially empty BST:
What is the height of the tree?

9 BST class // Interface (BST.h) #ifndef _BST_H #define _BST_H // include TreeNode struct class BST { public: BST(); ~BST(); void add(int value); int getMin() const; bool isEmpty() const; void print() const; bool contains(int value) const; void remove(int value); private: TreeNode* root; bool contains(TreeNode* node, int value) const; void print(TreeNode* node) const; void add(TreeNode* node, int value); }; #endif

10 add Method Add a new value to the BST so that the resulting tree is also a BST Where do we add 7? 14? 39? Typical approach for recursive tree method: member function that calls helper only private helper member function: takes same args plus a node pointer 25 15 45 8 23 33 58 13

11 BST Implementation BST:BST() { root = NULL; }
void BST::add(int value) { add(root, value); void BST::add(TreeNode* node, int value) { if(node == NULL) node = new TreeNode(value); else if(value < nodedata) add(nodeleft, value); else if(value > nodedata) add(noderight, value); Oops: modifying the NULL pointer doesn't change the tree How do we make the base case work? Setting node modifies local variable, but doesn't modify existing tree. Need to modify the root Or modify an existing node's left or right pointer

12 add Method: What's Wrong?
void BST::add(int value) { add(root, value); } void BST::add(TreeNode* node, int value) { if(node == NULL) node = new TreeNode(value); else if(value < nodedata) add(nodeleft, value); else if(value > nodedata) add(noderight, value); call tree.add(24) 25 15 45 8 23 33 58 24 13 node

13 BST Implementation void BST::add(int value) {
if(root == NULL) root = new TreeNode(value); else add(root, value); } void BST::add(TreeNode* node, int value) { // private if(value < nodedata){ if(nodeleft == NULL) nodeleft = new TreeNode(value); else add(nodeleft, value); else if(value > nodedata){ if(noderight == NULL)noderight = new TreeNode(value); else add(noderight, value); We addressed this type of issue in linked lists

14 BST Implementation void BST::add(int value) { add(root, value); }
void BST::add(TreeNode*& node, int value) { if(node == NULL) node = new TreeNode(value); else if(value < nodedata) add(nodeleft, value); else if(value > nodedata) add(noderight, value); How do we make the base case work? Pass the current node pointer by reference for changes to affect tree Recall: The difference in a regular parameter and a reference parameter

15 add Method: Second Approach
void BST::add(int value) { add(root, value); } void BST::add(TreeNode*& node, int value) { if(node == NULL) node = new TreeNode(value); else if(value < nodedata) add(nodeleft, value); else if(value > nodedata) add(noderight, value); call tree.add(24) We wanted to modify a pointer in the tree- So pass the pointer by reference 25 15 45 8 23 33 58 node 24 13

16 getMin Method getMin returns smallest value in binary search tree
throws exception if tree is empty

17 getMin Method getMin returns the minimum value in the binary search tree throws exception if tree is empty /* Returns minimum value in BST. If BST is empty, throws a string exception. */ int BST::getMin() const { if(root == NULL) throw "Cannot get minimum in empty BST"; return getMin(root); } // Add helper to .h file int BST::getMin(TreeNode* node) const { // private helper method if(node  left == NULL) return nodedata; else return getMin(node  left);

18 contains Method contains(value): returns true if value is in binary search tree, and false otherwise Like many tree methods, requires a private helper method that takes a node pointer as an additional argument Exercise: Write the contains Method

19 remove Method remove: BST member function
Removes specified integer from BST (if it's in the tree) Removal must result in a BST

20 remove Method tree.remove(33) tree.remove(8) tree.remove(45) 25 15 45
23 33 58 13

21 Removal remove a leaf: replace with NULL
remove node with left child only: replace with left child remove node with right child only: replace with right child remove node with two children: replace with min from right subtree Could also replace with max from left subtree 55 50 tree.remove(50); 30 60 30 60 21 42 80 21 42 55 80

22 remove Method Method removes the specified value from the BST if it is present void BST::remove(int value) { remove(root, value); // call helper on root of tree } // Need to modify tree – pass node pointer by reference void BST::remove(TreeNode*& node, int value){

23 remove Helper // right child only case
void BST::remove(TreeNode*& node, int value){ if(node == NULL) { // value isn't in tree } else if(value < nodedata) remove(nodeleft, value); else if(value > nodedata) remove(noderight, value); else { // nodedata == value, remove this node. TreeNode* delNode = NULL; if(nodeleft == NULL && noderight == NULL) { // leaf case delNode = node; node = NULL; else if(noderight == NULL) { // left child only case node = nodeleft; else if(nodeleft == NULL) { // right child only case delNode = node; node = noderight; } else { //case: left and right subtrees. nodedata = getMin(noderight); int minVal = nodedata; remove(noderight, minVal); if(delNode != NULL) delete delNode;

24 Destructor Delete all nodes

25 Destructor Delete all nodes BST::~BST() { destroy(root); }
void BST::destroy(TreeNode* node) { if(node != NULL) { destroy(node->left); destroy(node->right); delete node;


Download ppt "Binary Search Trees."

Similar presentations


Ads by Google