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.

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

Trees Types and Operations
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 Binary Search Trees.  Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
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.
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
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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.
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)
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Binary Search Trees Chapter 7 Objectives
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Cinda Heeren / Geoffrey Tien
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Trees.
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Trees.
COMP 103 Binary Search Trees.
Binary Search Trees.
Tree data structure.
Binary Trees, Binary Search Trees
Binary Search Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees.
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
Trees.
Tree traversals BST properties Search Insertion
CS 261 – Data Structures Binary Search Trees.
Binary Trees, Binary Search Trees
CS 261 – Data Structures AVL Trees.
Presentation transcript:

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 an implementation of a BAG Goal: try to make the operations on the collection proportional to the length of a path, rather than the number of elements in the tree. First step. A Binary Search Tree.

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);

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( ?? )

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 start, Object newValue) if start 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 Class BST { … public void add (EleType newValue) { root = add(root, newValue); dataSize++; } … } What is the complexity? O( ?? )

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

Remove: again 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

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?? Questions?? Now the worksheet.