CS 261 – Fall 2009 Binary Search Trees Again, but in detail.

Slides:



Advertisements
Similar presentations
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS261 Data Structures Binary Search Trees II Bag Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
CS261 Data Structures Binary Search Trees Concepts.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
CS 261 – RECITATION 5 SPRING OREGON STATE UNIVERSITY.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 25 Binary Search Trees
Binary Trees and Binary Search Trees
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
COMP 103 Binary Search Trees.
ITEC 2620M Introduction to Data Structures
Revised based on textbook author’s notes.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Search Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 Binary Search Trees.
Lecture 12 CS203 1.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
CMSC 341 Binary Search Trees.
CSC 143 Binary Search Trees.
Trees.
CS 261 – Data Structures Binary Search Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

CS 261 – Fall 2009 Binary Search Trees Again, but in detail

Binary Search Tree Binary search trees are binary tree’s where every node’s object value is: –Greater than or equal to all its descendents in the left subtree –Less than or equal to all its descendents in the right subtree An in-order traversal will return the elements in sorted order If the tree is reasonably full, searching for an element is O(log n)

Binary Search Tree: Example AlexAbnerAngelaAdelaAliceAudreyAdamAgnesAllenArthurAbigail

Binary Search Tree (BST): Implementation struct BST { struct Node * root; int size; }; struct node { EleType value; struct node * left; struct node * right; }; void BSTinit (struct BST *tree); void BSTadd(struct BST *tree, EleType value); int BSTcontains (struct BST *tree, EleType value); void BSTremove (struct BST *tree, EleType value); int BSTsze (struct BST *tree);

Init - what needs to be done? struct BST { struct node * root; int size; }; void BSTinit (struct BST *tree) { tree->root = 0; tree->size = 0; }

Implementing the Bag: Contains Start at the root. At each node, compare to test: return true if match If test is less than node, look at left child Otherwise if test is greater than node, look at right child Traverses a path from the root to the leaf. Therefore, if the tree is reasonably complete (an important if) the execution time is O( ?? )

Use Recursion, or use a loop? Both will work. Lets compare the two int BSTcontains (struct BST *tree, EleType value) { struct node * current = tree->root; while (current != 0) { if (EQ(value, current->value)) return 1; if (LT(value, current->value)) current = current->left; else current = current->right; } return 0; }

Recursive version int BSTcontains (struct BST *tree, EleType value) { return BSTnodecontains(tree->root, value); } int BSTnodeContains (struct BST *current, EleType value) { if (current == 0) return 0; if (EQ(value, current->value)) return 1; if (LT(value, current->value)) return BSTnodeContains(current->left, value); else return BSTnodeContains(current->right, value); }

Which is easier to understand? Somewhat a matter of style It is what you are used to Execution time will be very similar

Implementing a Bag: Add Do the same type of traversal from root to leaf. When you find a null value, create a new node. AlexAbnerAngelaAdelaAliceAudreyAdamAgnesAllenArthurAbigail

A useful trick A useful trick (adapted from the functional programming world). Make a secondary routine that returns the tree with the value inserted. Node add (Node current, EleType newValue) if current is null then return new Node with value otherwise if newValue < Node value left child = add (left child, newValue) else right child = add (right child, newValue) return current node

Add just calls the utility routine void BSTadd (EleType newValue) { root = _BSTnodeAdd(root, newValue); dataSize++; }

Real work is done inside Node struct node * BSTnodeAdd (struct node * current, EleType value) { if (current == 0) { current = (struct node *) malloc(sizeof(struct node)); assert(current != 0); current->value = value; current->left = current->right = 0; } else if (LT(value, current->value)) current->left = BSTnodeAdd(current->left, value); else current->right = BSTnodeAdd(current->right, value); return current; }

Notes on Add See how it returns the Current value, the tree AFTER the insertion is made Again, could be done with a loop - some people find the recursive version easier, some people find the loop easier. Try writing it using a loop

Bag Implementation: Remove As is often the case, remove is the most complex. Leaves a “hole”. What value should fill the hole? AlexAbnerAngelaAdelaAliceAudreyAdamAgnesAllenArthurAbigail

Who can fill that hole? Answer: The Leftmost child of the right child. (Smallest element in right subtree) Try this on a few values. Useful to have a couple of private inner routines EleType leftmostChild (Node current) { … // return value of leftmost child of current } Struct Node * removeLeftmostChild (Node current) { … // return tree with leftmost child removed }

One additional special case We have said you want to fill hole with leftmost child of right child. What if you don’t have a right child? Think about it. Can just return left child. Try remove “Audrey” AngelaAliceAudreyAllenArthur

Once more, separate root and node void BSTremove (struct BST * tree, EleType value) { if (BSTincludes(tree, value)) { tree->root = BSTnodeRemove (tree->root, value); tree->size--; }

Node Remove: easy if you return a tree Node remove (Node current, EleType testValue) if current->value is the thing we seek if right child is null return left child else replace value with leftmost child of right child and set right child to be removeLeftmost (right) else if testValue < current.value left child = remove (left child, testValue) else right child = remove (right child, testValue) return current node

Translate the previous into code struct BSTnodeRemove (struct node * current, EleType testValue) { if (EQ(testValue, current->value)) { /* found it! */ if (current->right == 0) return current->left; else { current->value = leftmostChild(current->right); current->right = removeLeftMost (current->right); } } else if (LT(testValue, current.value)) current->left = BSTnodeRemove (current->left, testValue) else current->right = BSTnoderemove (current->right, testValue) return current; }

Little Helper Routines - get value of lmc EleType leftMostChild (struct node * current) { while (current->left != 0) current = current->left; return current->value; }

Second helper routine - remove leftmost struct node * removeLeftMost (struct node * current) { if (current->left == 0) return current->right; else current->left = removeLeftMost (current->left); return current; }

What is the complexity?? Basically once more just running down a path from root to leaf What is the complexity? O(???) Careful! What can go wrong? What happens in the worst case??