Trees Trees.

Slides:



Advertisements
Similar presentations
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
BST Data Structure A BST node contains: A BST contains
Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?
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.
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 Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
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)
Lecture 17 Non-Linear data structures Richard Gesick.
Building Java Programs Binary Search Trees; TreeSet.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
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."
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CSE 373 Binary search trees; tree height and balance
Tree Insert Animation.
Recursive Objects (Part 4)
BST Trees
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Binary Search Tree (BST)
Binary Search Trees.
Trees.
Topic 18 Binary Search Trees
Binary Search Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Search Trees.
Chapter 21: Binary Trees.
Building Java Programs
Binary Search Tree AVL Tree
Node Removal From BST Source:
CSE 373: Data Structures and Algorithms
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Lecture 21: Binary Search Trees; TreeSet
CSE 373 Data Structures and Algorithms
BINARY TREE CSC248 – Data Structure.
CSC 143 Binary Search Trees.
Trees.
Lecture 21: Binary Search Trees; TreeSet
Data Structures II AP Computer Science
Trees.
Lecture 10: BST and AVL Trees
Lecture 9: Intro to Trees
Non-Linear data structures
COSC2007 Data Structures II
Presentation transcript:

Trees Trees

a binary tree a Binary search Tree not a tree Some Trees and Graphs root root root 9 6 5 3 8 7 root root 6 6 5 6 8 7 7 8 5 4 3 2 8 3 7 7 a binary tree a tree a tree and a ... a Binary search Tree not a tree Terminology 9 6 5 3 8 7 root A Tree - each child node has a single parent node, except for the root which has no parent parent nodes children nodes right sub tree left sub tree Trees leaf nodes

Binary Trees root A tree where each node has at most 2-children 9 6 5 3 8 7 root left right data A tree where each node has at most 2-children Binary Search Trees(BST) A binary search tree is a binary tree where the elements of the left sub-tree are smaller than the root, and the elements of the right sub-tree are larger than the root. Standard processes on a BST: public interface BinarySearchTree{ public void inOrderTraversal(); public void preOrderTraversal(); public void postOrderTraversal(); public void insert(Comparable item); public boolean search(Comparable item); public void remove(Comparable item); } Traversals x 3 Insert to tree Remove from tree Search tree Trees

BST Operations BST t = new BST(); root boolean b = t.search(new Integer(3)); 4 9 6 3 8 7 root 5 root 6 t.insert(new Integer(6)); root 6 t.insert(new Integer(4)); 4 root 6 b = t.search(new Integer(5)); 4 4 9 6 3 8 7 root 5 t.insert(new Integer(3)); root 3 6 t.insert(new Integer(8)); 4 8 3 root 6 t.insert(new Integer(8)); 4 8 b = t.search(new Integer(2)); 4 9 6 3 8 7 root 5 3 root 6 t.insert(new Integer(5)); 4 8 3 5 Trees

BST Operations - Traversals 4 9 6 3 8 7 root 5 Post-order Traversal: left sub tree - Right sub tree - Root 3 - 5 - 4 - 7 - 9 - 8 - 6 Pre-order Traversal: Root - left sub tree - Right sub tree 6 - 4 - 3 - 5 - 8 - 7 - 9 In-order Traversal: left sub tree - Root - Right sub tree 3 - 4 - 5 - 6 - 7 - 8 - 9 What traversal technique should be employed if you wanted to save the contents of the tree in order to recreate the same tree at a later time? Trees

BST Operations - Removing a node 6 4 5 9 3 2 6 4 5 9 3 2 remove(2) => no children 6 4 5 9 3 2 6 4 5 9 3 2 remove(3) => one child 6 4 5 9 3 2 6 4 5 9 3 2 remove(6) two children Replace by the right-most node in the left sub-tree Trees

Binary Search Trees Animation of In order Traversal Trees left right data import java.util.*; public class BST implements BinarySearchTree{ class TreeNode{ TreeNode left; TreeNode right; Comparable data; TreeNode(Comparable d){ data = d; left = null; right = null;} } TreeNode root; public BST(){ root = null;} public void inOrderTraversal(){ inOrderTraversalHelper(root);} private void inOrderTraversalHelper(TreeNode node){ if (node != null){ inOrderTraversalHelper(node.left); System.out.println(node.data.toString()); inOrderTraversalHelper(node.right); //pre and post order traversals very similar to inorder public boolean search(Comparable item){ return searchHelper(item,root); private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); Animation of In order Traversal Trees

Binary Search Trees Trees public void insert(Comparable item){ if (search(item)) return;//prevent duplicates root = insertHelper(item,root); } private TreeNode insertHelper(Comparable item, TreeNode node){ if (node == null) return new TreeNode(item); if (node.data.compareTo(item)>0) node.left = insertHelper(item,node.left); else node.right = insertHelper(item,node.right); return node; public void remove(Comparable item) throws NoSuchElementException{ root = removeHelper(item,root); private TreeNode removeHelper(Comparable item, TreeNode node) throws NoSuchElementException{ if (node == null) throw new NoSuchElementException(); if (node.data.compareTo(item) == 0){ if (node.left == null) return node.right; if (node.right == null) return node.left; Comparable data = findRightMostDataValue(node.left); node.left = removeHelper(data, node.left); node.data = data; if (node.data.compareTo(item) > 0) node.left = removeHelper(item, node.left); if (node.data.compareTo(item) < 0) node.right = removeHelper(item, node.right); private Comparable findRightMostDataValue(TreeNode node){ if (node.right == null) return node.data; else return findRightMostDataValue(node.right); Trees

Why Trees? Improve Insert/Search/Delete performance over linear structures. If the tree is “well balanced”, then expected performance: Insert/Delete/Search O(log2n) (Under what circumstance?) Worst case times for a BST: Insert/Delete/Search O(n) (Under what circumstance?) Other tree structures: Goal is to grow a well balanced trees that are not too tall Trees where nodes have more than 2-children Trees that detect and modify themselves when they become unbalanced Trees that are grown from the bottom up - thereby ensuring balanced growth. Trees