TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.

Slides:



Advertisements
Similar presentations
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Advertisements

Chapter 3 Collection Classes Anshuman Razdan Department of Engineering
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.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Linklist21 Linked Lists II The rest of the story.
Chapter 4 Linked Lists 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.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
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.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
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.
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.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
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.
Building Java Programs Binary Search Trees; TreeSet.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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 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.
CS261 Data Structures Binary Search Trees II Bag Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
Binary Search Trees (BST) Let’s look at some pics …and some code.
Lecture 9 Binary Trees Trees General Definition Terminology
CS261 Data Structures Binary Search Trees Concepts.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Recursive Objects (Part 4)
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Trees.
Trees.
COMP 103 Binary Search Trees.
Topic 18 Binary Search Trees
Binary Search Trees.
Binary Search Trees Definition (recursive):
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.
Depict the Tree Structure in a picture
Topic 19 Binary Search Trees
Trees 3: The Binary Search Tree
Node Removal From BST Source:
Binary Search Trees A special case of a Binary Tree
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSC 143 Binary Search Trees.
Building Java Programs
Trees.
CS 261 – Data Structures Binary Search Trees.
Trees Trees.
Presentation transcript:

TreeBag a BST implementation

Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void add(Comparable element) public void addAll(TreeBag addend) public Object clone( ) public int countOccurrences(Comparable target) private boolean remove(Comparable target) public int size( ) public static TreeBag union(TreeBag b1, TreeBag b2)

Some BSTNode methods public boolean search(Comparable obj) public boolean insert(Comparable obj) public int size() public boolean isLeaf() public BSTNode getLeftMostData() public BSTNode removeLeftMost() public void printPreOrder() public static int height()

TreeBag class treeBagroot BSTNode TreeBag

TreeBag class data is stored in InOrder in the binary search tree (order not required for Bag) repeated values x are stored (arbitrarily) in right subtree of original value x effect on delete(x) effect on countOccurrences(x)

TreeBag class repeated values are leftmost elements of right subtree of value repeated 10

class TreeBag public class TreeBag implements Cloneable { BSTNode root; public TreeBag() { root = null; } … }

TreeBag - add public class TreeBag implements Cloneable { BSTNode root; … public void add (Comparable element) { if (root==null) root = new BSTNode(element,null,null); else root.insert(element); }

TreeBag - countOccurrences public int countOccurrences(Comparable target) { // assume repeated values are inserted after int count=0; BSTNode cursor = root; while (cursor != null) { if (cursor.getData( ).compareTo(target)>0) cursor = cursor.getLeft( ); else { if (cursor.getData( ).compareTo(target)==0) count++; cursor = cursor.getRight( ); } return count; } // write recursive version of countOccurrences()?

TreeBag - remove public boolean remove(Comparable target) // method completely in TreeBag class { BSTNode parentOfCursor = null; BSTNode cursor = root; while (cursor != null && cursor.getData().compareTo(target)!=0) { parentOfCursor = cursor; if (cursor.getData().compareTo(target)>0) cursor = cursor.getLeft( ); else cursor = cursor.getRight( ); } if (cursor == null) return false; // target not found if (cursor.getRight( ) == null) { if (parentOfCursor == null) // removing root root = cursor.getLeft( ); else if (cursor == parentOfCursor.getLeft( )) parentOfCursor.setLeft(cursor.getLeft( )); else parentOfCursor.setRight(cursor.getLeft( )); } // could include another test for right child == null else // replacing with data from next node { cursor.setData(cursor.getRight( ).getLeftmostData( )); cursor.setRight(cursor.getRight( ).removeLeftmost( )); } return true; }

TreeBag clone public Object clone( ) { TreeBag answer; try { answer = (TreeBag) super.clone( ); } catch (CloneNotSupportedException e) {throw new InternalError(e.toString( )); } answer.root = BSTNode.treeCopy(root); return answer; }

BSTNode - treeCopy public static BSTNode treeCopy(BSTNode source) { if (source == null) return null; BSTNode copy = new BSTNode(source.data,null,null); if (source.left != null) copy.left = treeCopy(source.left); if (source.right != null) copy.right = treeCopy(source.right); return copy; }

TreeBag - addAll TreeBag t1 root TreeBag t2 root t1.addAll(t2); TreeBag t1 root TreeBag t2 root

TreeBag - addAll public void addAll(TreeBag addend) { BSTNode addroot; if (addend == null) { throw new IllegalArgumentException("Null addend"); } if (root == addend.root) { // Addend is same as bag that activated method addroot = BSTNode.treeCopy(addend.root); addTree(addroot); } else addTree(addend.root); }

TreeBag - addTree private void addTree(BSTNode addroot) { if (addroot != null) { add(addroot.getData( )); addTree(addroot.getLeft( )); addTree(addroot.getRight( )); }

TreeBag - union public static TreeBag union(TreeBag b1, TreeBag b2) { TreeBag answer = new TreeBag( ); answer.addAll(b1); answer.addAll(b2); return answer; }