a graph is a data structure made up of nodes in graph theory the links are normally called edges

Similar documents
from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

Programming II (CS300)

Associate Professor Dr. Raed Ibraheem Hamed

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Data Structures and Algorithms

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Chapter 20: Binary Trees

TREES. Trees - Introduction

Lecture 23: Binary Search Trees

Principles of Computer Science

CS350: Data Structures Binary Search Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

CS 206 Introduction to Computer Science II

Trees. Truong Tuan Anh CSE-HCMUT

CS 206 Introduction to Computer Science II

Binary Trees, Binary Search Trees

Binary Search Trees. See Section 11.1 of the text.

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CS 350 : Data Structures Binary Search Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

INF2220: algorithms and data structures Series 1

CS350: Data Structures Tree Traversal

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Spring 2018 Mentoring 8: March 14, Binary Trees

Trees. (Trees) Data Structures and Programming Spring / 28

Binary Trees

CS 127 Fall In our last episode. Yet more fun with binary trees. Tree traversals. Breadth-first traversal

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CMSC 341. Binary Search Trees CMSC 341 BST

We have the pointers reference the next node in an inorder traversal; called threads

The Binary Search Tree ADT

Binary Trees. Examples:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Successor/Predecessor Rules in Binary Trees

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Trees. Tree Structure Binary Tree Tree Traversals

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

A set of nodes (or vertices) with a single starting point

Binary Trees Fall 2018 Margaret Reid-Miller

ITI Introduction to Computing II

» Access elements of a container sequentially without exposing the underlying representation

CS24 Week 8 Lecture 1

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary Trees and Binary Search Trees

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Programming II (CS300)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Binary Search Trees. What is a Binary Search Tree?

ITI Introduction to Computing II

Chapter 5. Binary Trees

Data Structures And Algorithms

Binary Search Tree (3A) Young Won Lim 6/6/18

Binary search trees (BST) Binary search trees (BST)

CSI33 Data Structures

CS 206 Introduction to Computer Science II

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Trees, Binary Trees, and Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Tree (3A) Young Won Lim 6/2/18

Binary Search Tree (3A) Young Won Lim 6/4/18

CSE 230 Intermediate Programming in C and C++ Binary Tree

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Binary Trees. Height 1

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Name CPTR246 Spring '17 (100 total points) Exam 3

CmpSci 187: Programming with Data Structures Spring 2015

CIS 121. Binary Search Trees

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Partha Sarathi Mandal

CE 221 Data Structures and Algorithms

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

CMSC 341 Lecture 10 Binary Search Trees

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Tree Travsersals and BST Iterators

Topic 14. The BinaryTree ADT

Multi-Way Search Tree

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Programming II (CS300)

tree nonlinear Examples

Friday Four Square! 4:15PM, Outside Gates

Binary Search Trees 1

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Algorithms. Deleting from Red-Black Trees B-Trees

Garbage Collection: recycling unused memory

Transcription:

1 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/ 2

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 3

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

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

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

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

Trees a node without any children is called a leaf 8

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

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

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

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

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

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

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

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 16

50 left right 27 73 8 44 83 73 93

50 left 27 73 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 21

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

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) { } return true; boolean inrighttree = contains(element, node.right); return inrighttree; is tree empty? examine root examine left subtree examine right subtree 23

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

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

50 27 == 93? 27 73 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 1. inorder recursively traverse the left subtree, then visit the node, then recursively traverse the right subtree 33

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

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

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 37

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? 39

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 40

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

42 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: 1. all nodes in the left subtree have data elements that are less than the data element of the root node 2. all nodes in the right subtree have data elements that are greater than or equal to the data element of the root node 3. rules 1 and 2 apply recursively to every subtree 44

left subtree (all elements < 50) 50 right subtree (all elements >= 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? 46

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 47

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

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 49

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; } 50

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

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

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: 1. all nodes in the left subtree have data elements that are less than the data element of the root node 2. all nodes in the right subtree have data elements that are greater than the data element of the root node 3. rules 1 and 2 apply recursively to every subtree 53

/** * 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); 54

/** * 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 } else { BinarySearchTree.add(element, root.right); // recursively add to the right subtree } } } 55

/** * 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 } else { BinarySearchTree.add(element, root.right); // recursively add to the right subtree } } } 56

/** * 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 } else { BinarySearchTree.add(element, root.right); // recursively add to the right subtree } } } 57

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 58

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

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

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 61

/** * 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; } base case? } recursive case? return BinarySearchTree.minimumInSubtree(root.left); 62

/** * 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; } base case? } recursive case? return BinarySearchTree.maximumInSubtree(root.right); 63

/** * 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); } 64

/** * 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); } 65

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

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 67

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 70

50 27 73 delete 83 8 44 51 83 74 76

50 27 73 8 44 74 51 76

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

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 74

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

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

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

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

44 27 73 8 51 74 76

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

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

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

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

51 27 73 8 44 74 76