Trees.

Slides:



Advertisements
Similar presentations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
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.
CS 171: Introduction to Computer Science II
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Binary Search Trees Chapter 7 Objectives
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Chapter 20 Binary Search Trees
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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)
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
Binary Search Trees Chapter 7 Objectives
Non Linear Data Structure
Chapter 25 Binary Search Trees
CSE 373 Binary search trees; tree height and balance
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Data Structures & Algorithm Design
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
CMSC 341 Binary Search Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Lecture 12 CS203 1.
CMSC 341 Binary Search Trees.
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Trees.
Chapter 20: Binary Trees.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees

Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in a wide variety of real-world problems social network analysis e.g., six-degrees-of-Kevin-Bacon, Lost Circles transportation networks e.g., http://ac.fltmaps.com/en many other examples http://www.visualcomplexity.com/vc/

Trees trees are special cases of graphs a tree is a data structure made up of nodes each node stores data each node has links to zero or more nodes in the next level of the tree children of the node each node has exactly one parent node except for the root node

50 34 11 6 79 23 99 33 67 88 31 1 6 83

50 11 6 79 34 88 67 23 33 99 1 31 83

Trees the root of the tree is the node that has no parent node all algorithms start at the root

root 50 34 11 6 79 23 99 33 67 88 31 1 6 83

Trees a node without any children is called a leaf

50 34 11 6 79 23 99 33 67 88 31 1 6 83 leaf leaf leaf leaf leaf leaf

Trees the recursive structure of a tree means that every node is the root of a tree

50 34 11 6 79 23 99 33 67 88 31 1 subtree 6 83

50 34 11 6 79 23 99 33 67 88 31 1 subtree 6 83

50 subtree 34 11 6 79 23 99 33 67 88 31 1 6 83

50 34 11 6 subtree 79 23 99 33 67 88 31 1 6 83

50 34 11 6 79 23 99 33 67 88 subtree 31 1 6 83

Binary Tree a binary tree is a tree where each node has at most two children very common in computer science many variations traditionally, the children nodes are called the left node and the right node

50 left right 27 73 8 44 83 73 93

50 27 73 left right 8 44 83 73 93

50 27 73 right 8 44 83 73 93

50 27 73 8 44 83 left right 74 93

Binary Tree Algorithms the recursive structure of trees leads naturally to recursive algorithms that operate on trees for example, suppose that you want to search a binary tree for a particular element

public static <E> boolean contains(E element) { return contains(element, this.root); }

is tree empty? examine root examine left subtree examine right subtree private static <E> boolean contains(E element, Node<E> node) { if (node == null) { return false; } if (element.equals(node.data)) { return true; boolean inLeftTree = contains(element, node.left); if (inLeftTree) { boolean inRightTree = contains(element, node.right); return inRightTree; is tree empty? examine root examine left subtree examine right subtree

t.contains(93) 50 27 73 8 44 83 74 93

50 50 == 93? 27 73 8 44 83 74 93

50 27 73 27 == 93? 8 44 83 74 93

50 27 73 8 44 83 8 == 93? 74 93

50 27 73 8 44 83 44 == 93? 74 93

50 27 73 73 == 93? 8 44 83 74 93

50 27 73 8 44 83 83 == 93? 74 93

50 27 73 8 44 83 74 93 74 == 93?

50 27 73 8 44 83 74 93 93 == 93?

Iteration or Traversal visiting every element of the tree can also be done recursively 3 possibilities based on when a node is visited inorder recursively traverse the left subtree, then visit the node, then recursively traverse the right subtree

50 27 73 8 44 83 74 93 inorder: 8, 27, 44, 50, 73, 74, 83, 93

Iteration or Traversal preorder visit the node, then recursively traverse the left subtree, then recursively traverse the right subtree

50 27 73 8 44 83 74 93 preorder: 50, 27, 8, 44, 73, 83, 74, 93

Iteration or Traversal postorder recursively traverse the left subtree, then recursively traverse the right subtree, then visit the node

50 27 73 8 44 83 74 93 postorder: 8, 44, 27, 74, 93, 83, 73, 50

Iteration or Traversal what kind of traversal is contains?

Iteration or Traversal the previous three tree traversals are all depth-first traversals called depth first because for any node you traverse the entire left subtree before traversing the right subtree another possible traversal is to visit all nodes at the same level before continuing on the next lower level called breadth first search

50 27 73 8 44 83 74 93 breadth first: 50, 27, 73, 8, 44, 83, 74, 93

Binary Search Trees

50 27 73 8 44 83 74 93

Binary Search Trees (BST) the tree from the previous slide is a special kind of binary tree called a binary search tree in a binary search tree: all nodes in the left subtree have data elements that are less than the data element of the root node all nodes in the right subtree have data elements that are greater than or equal to the data element of the root node rules 1 and 2 apply recursively to every subtree

50 27 73 8 44 51 83 74 93 76 right subtree (all elements >= 50) left subtree (all elements < 50) 50 27 73 8 44 51 83 74 93 76

Binary Search Trees (BST) is every node of a BST the root of a BST?

Implementing a BST what types of data elements can a BST hold? hint: we need to be able to perform comparisons such as less than, greater than, and equal to with the data elements

E must implement Comparable<E> public class BinarySearchTree<E extends Comparable<E>> { E must implement Comparable<E> “for every type E that can be compared to itself”

Implementing a BST: Nodes we need a node class that: has-a data element has-a link to the left subtree has-a link to the right subtree

public class BinarySearchTree<E extends Comparable<E>> { private static class Node<E> { private E data; private Node<E> left; private Node<E> right; /** * Create a node with the given data element. The left and right child * nodes are set to null. * * @param data * the element to store */ public Node(E data) { this.data = data; this.left = null; this.right = null; }

Implementing a BST: Fields and Ctor a BST has-a root node creating an empty BST should set the root node to null

/. The root node of the binary search tree /** * The root node of the binary search tree. */ private Node<E> root; * Create an empty binary search tree. public BinarySearchTree() { this.root = null; }

Implementing a BST: Adding elements the definition for a BST tells you everything that you need to know to add an element in a binary search tree: all nodes in the left subtree have data elements that are less than the data element of the root node all nodes in the right subtree have data elements that are greater than the data element of the root node rules 1 and 2 apply recursively to every subtree

/. Add an element to the tree /** * Add an element to the tree. The element is inserted into the tree in a * position that preserves the definition of a binary search tree. * * @param element * the element to add to the tree */ public void add(E element) { if (this.root == null) { this.root = new Node<E>(element); } else { // call recursive static method BinarySearchTree.add(element, null, this.root);

/. Add an element to the tree with the specified root /** * Add an element to the tree with the specified root. The element is inserted into the * tree in a position that preserves the definition of a binary search tree. * * @param element the element to add to the subtree * @param root the root of the subtree */ private static <E extends Comparable<E>> void add(E element, Node<E> root) { if (element.compareTo(root.data) < 0) { // element belongs in the left subtree if (root.left == null) { // is there no left subtree? root.left = new Node<E>(element); // add the element as the new left child } else { BinarySearchTree.add(element, root.left); // recursively add to the left subtree } } else { // element belongs in the right subtree if (root.right == null) { // is there no right subtree? root.right = new Node<E>(element); // add the element as the new right child BinarySearchTree.add(element, root.right); // recursively add to the right subtree

/. Add an element to the tree with the specified root /** * Add an element to the tree with the specified root. The element is inserted into the * tree in a position that preserves the definition of a binary search tree. * * @param element the element to add to the subtree * @param root the root of the subtree */ private static <E extends Comparable<E>> void add(E element, Node<E> root) { if (element.compareTo(root.data) < 0) { // element belongs in the left subtree if (root.left == null) { // is there no left subtree? root.left = new Node<E>(element); // add the element as the new left child } else { BinarySearchTree.add(element, root.left); // recursively add to the left subtree } } else { // element belongs in the right subtree if (root.right == null) { // is there no right subtree? root.right = new Node<E>(element); // add the element as the new right child BinarySearchTree.add(element, root.right); // recursively add to the right subtree

/. Add an element to the tree with the specified root /** * Add an element to the tree with the specified root. The element is inserted into the * tree in a position that preserves the definition of a binary search tree. * * @param element the element to add to the subtree * @param root the root of the subtree */ private static <E extends Comparable<E>> void add(E element, Node<E> root) { if (element.compareTo(root.data) < 0) { // element belongs in the left subtree if (root.left == null) { // is there no left subtree? root.left = new Node<E>(element); // add the element as the new left child } else { BinarySearchTree.add(element, root.left); // recursively add to the left subtree } } else { // element belongs in the right subtree if (root.right == null) { // is there no right subtree? root.right = new Node<E>(element); // add the element as the new right child BinarySearchTree.add(element, root.right); // recursively add to the right subtree

Predecessors and Successors in a BST in a BST there is something special about a node's: left subtree right-most child right subtree left-most child

50 27 73 8 44 51 83 74 93 76 left subtree (all elements < 50) rightmost child 74 93 76 left subtree rightmost child = inorder predecessor

50 27 73 8 44 51 83 74 93 76 right subtree (all elements > 50) leftmost child 74 93 76 right subtree leftmost child = inorder successor

Predecessors and Successors in a BST in a BST there is something special about a node's: left subtree right-most child = inorder predecessor the node containing the largest value less than the root right subtree left-most child = inorder successor the node containing the smallest value greater than the root it is easy to find the predecessor and successor nodes if you can find the nodes containing the maximum and minimum elements in a subtree

base case? recursive case? /** * Find the node in a subtree that has the smallest data element. * * @param root * the root of the subtree * @return the node in the subtree that has the smallest data element. */ public static <E> Node<E> minimumInSubtree(Node<E> root) { if (root.left == null) { return root; } return BinarySearchTree.minimumInSubtree(root.left); base case? recursive case?

base case? recursive case? /** * Find the node in a subtree that has the largest data element. * * @param root * the root of the subtree * @return the node in the subtree that has the largest data element. */ public static <E> Node<E> maximumInSubtree(Node<E> root) { if (root.right == null) { return root; } return BinarySearchTree.maximumInSubtree(root.right); base case? recursive case?

/** * Find the node in a subtree that is the predecessor to the root of the * subtree. If the predecessor node exists, then it has the * largest data element in the left subtree of root. * * @param root * the root of the subtree * @return the node in a subtree that is the predecessor to the root of * the subtree, or null if the root of the subtree * has no predecessor */ public static <E> Node<E> predecessorInSubtree(Node<E> root) { if (root.left == null) { return null; } return BinarySearchTree.maximumInSubtree(root.left);

/. Find the node in a subtree that is the successor to the root of the /** * Find the node in a subtree that is the successor to the root of the * subtree. If the successor node exists, then it is the node that has * the smallest data element in the right subtree of root. * * @param root * the root of the subtree * @return the node in a subtree that is the successor to the root of * the subtree, or null if the root of the subtree has no * successor */ public static <E> Node<E> successorInSubtree(Node<E> root) { if (root.right == null) { return null; } return BinarySearchTree.minimumInSubtree(root.right);

Deletion from a BST to delete a node in a BST there are 3 cases to consider: deleting a leaf node deleting a node with one child deleting a node with two children

Deleting a Leaf Node deleting a leaf node is easy because the leaf has no children simply remove the node from the tree e.g., delete 93

50 27 73 8 44 51 83 delete 93 74 93 76

50 27 73 8 44 51 83 74 76

Deleting a Node with One Child deleting a node with one child is also easy because of the structure of the BST remove the node by replacing it with its child e.g., delete 83

50 27 73 delete 83 8 44 51 83 74 76

50 27 73 8 44 51 74 76

Deleting a Node with Two Children deleting a node with two children is a little trickier can you see how to do it?

Deleting a Node with Two Children replace the node with its inorder predecessor OR inorder successor call the node to be deleted Z find the inorder predecessor OR the inorder successor call this node Y copy the data element of Y into the data element of Z delete Y e.g., delete 50

delete 50 using inorder predecessor 27 73 8 44 51 74 76

50 Z 27 73 8 44 51 74 Y inorder predecessor to Z 76

44 27 73 8 44 51 74 76 copy Y data to Z data Z Y inorder predecessor

44 Z 27 73 delete Y 8 44 51 74 Y 76

44 27 73 8 51 74 76

delete 50 using inorder successor 27 73 8 44 51 74 76

50 Z 27 73 8 44 51 74 Y inorder successor to Z 76

51 27 73 8 44 51 74 76 copy Y data to Z data Z Y inorder successor

51 Z 27 73 delete Y 8 44 51 74 Y 76

51 27 73 8 44 74 76