CS261 Data Structures Binary Search Trees II Bag Implementation.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
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.
Binary Search Trees Chapter 7 Objectives
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.
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
Binary Trees. Node structure Data A data field and two pointers, left and right.
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.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Binary search trees. What’s on the menu? ADT DictionaryBST & Algorithms SimulatorComplexity.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
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
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
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.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Binary Search Trees (BST) Let’s look at some pics …and some code.
CS261 Data Structures Binary Search Trees Concepts.
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.
CS 261 – RECITATION 5 SPRING OREGON STATE UNIVERSITY.
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
The Tree Data Structure
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Week 6 - Wednesday CS221.
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
COMP 103 Binary Search Trees.
Binary Search Trees.
Tree data structure.
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Search Trees.
CSE 373 Data Structures and Algorithms
CSC 143 Binary Search Trees.
Trees.
CS 261 – Data Structures Binary Search Trees.
CS 261 – Data Structures AVL Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CS261 Data Structures Binary Search Trees II Bag Implementation

Goals BST Representation Bag Operations Functional-style operations

Binary Search Tree (review) Binary search trees are binary trees where every nodes object value is: – Greater than all its descendents in the left subtree – Less than or equal to all its descendents in the right subtree In-order traversal returns elements in sorted order If tree is reasonably full (well balanced), searching for an element is O(log n)

Binary Search Tree (review) AlexAbnerAngelaAdelaAliceAudreyAdamAgnesAllenArthurAbigail

BST Bag struct BSTree { struct Node *root; int cnt; }; void initBSTree(struct BSTree *tree); void addBSTree(struct BSTree *tree, TYPE val); int containsBSTree(struct BSTree *tree, TYPE val); void removeBSTree(struct BSTree *tree, TYPE val); int sizeBSTree(struct BSTree *tree); struct Node { TYPE val; struct Node *left struct Node *rght };

Functional Approach A useful trick (adapted from the functional programming world) : Recursive helper routine that returns tree with the value inserted Node addNode(Node current, TYPE value) if current is null then return new Node with value otherwise if value < Node.value left child = addNode(left child, value) else right child = addNode(right child, value) return current node

Visual Example

Process Stack

BST Add: public facing add void add(struct BSTree *tree, TYPE val) { tree->root = _addNode(tree->root, val); tree->cnt++; }

Recursive Helper – functional flavor struct Node *_addNode(struct Node *cur, TYPE val){ struct Node *newnode; if (cur == NULL){ /* base case goes here */ return newNode } if (val val) /* recursive call left */ else /*recursive call right */ return cur; }

Python Functional Version def binary_tree_insert(node, key, value): if node is None: return TreeNode(None, key, value, None) if key == node.key: return TreeNode(node.left, key, value, node.right) if key < node.key: return TreeNode(binary_tree_insert(node.left, key, value), node.key, node.value, node.right) else: return TreeNode(node.left, node.key, node.value, binary_tree_insert(node.right, key, value))

Iterative Flavor - Java public void insert(int data) { if (m_root == null) { m_root = new TreeNode(data, null, null); return; } Node root = m_root; while (root != null) { if (data == root.getData()) { return; } else if (data < root.getData()) { if (root.getLeft() == null) { root.setLeft(new TreeNode(data, null, null)); return; } else { root = root.getLeft(); }

Iterative (Java) } else { if (root.getRight() == null) { root.setRight(new TreeNode(data, null, null)); return; } else { root = root.getRight(); }

BST Remove AlexAbnerAngelaAdelaAliceAudreyAdamAgnesAllenArthurAbigail How would you remove Abigail? Audrey? Angela?

Who fills the hole? Answer: the leftmost child of the right subtree (smallest element in right subtree) Useful to have a couple of private inner routines: TYPE _leftmost(struct Node *cur) {... /* Return value of leftmost child of current node. */ } struct Node *_removeLeftmost(struct Node *cur) {... /* Return tree with leftmost child removed. */ }

Intuition…

BST Remove Example AlexAbnerAngelaAdelaAbigailAliceAudreyAdamAgnesAllenArthur Before call to remove Element to remove Replace with: leftmost(rght)

BST Remove Example AlexAbnerArthurAdelaAbigailAliceAdamAgnesAllenAudrey After call to remove

BST Remove Pseudocode Node removeNode(Node current, TYPE value) if value = current.value if right child is null return left child else replace value with value in leftmost child of right subtree set right child to result of removeLeftmost(right) else if value < current.value left child = removeNode(left child, value) else right child = removeNode(right child, value) return current node

Comparison Average Case Execution Times OperationDynArrBagLLBagOrdered ArrBag BST Bag AddO(1+)O(1)O(n)O(logn) ContainsO(n) O(logn) RemoveO(n) O(logn)

Space Requirements Does the functional-style recursive version require more or less space than an iterative version? Think about the call stack? – rebuilding as move up from recursion requires O(logn) space on average

Your Turn Complete the BST implementation in Worksheet #29