Principles of Computer Science

Similar documents
Binary Trees

Binary Trees, Binary Search Trees

Programming II (CS300)

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

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

Topic 14. The BinaryTree ADT

Binary Trees. Height 1

TREES. Trees - Introduction

Chapter 20: Binary Trees

CS 206 Introduction to Computer Science II

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

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

Why Do We Need Trees?

tree nonlinear Examples

Binary Trees. Examples:

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Programming II (CS300)

CS 206 Introduction to Computer Science II

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

BBM 201 Data structures

Trees. Truong Tuan Anh CSE-HCMUT

CS350: Data Structures Tree Traversal

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

INF2220: algorithms and data structures Series 1

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Data and File Structures Laboratory

Trees. Tree Structure Binary Tree Tree Traversals

Binary Trees Fall 2018 Margaret Reid-Miller

Trees, Binary Trees, and Binary Search Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

18. Binary Search Trees

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

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

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

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

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

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

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

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

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

CSC148 Week 6. Larry Zhang

Data Structure - Binary Tree 1 -

Algorithms. Deleting from Red-Black Trees B-Trees

Associate Professor Dr. Raed Ibraheem Hamed

Spring 2018 Mentoring 8: March 14, Binary Trees

March 20/2003 Jayakanth Srinivasan,

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

CSI33 Data Structures

CS302 - Data Structures using C++

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

Stacks, Queues and Hierarchical Collections

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

8. Binary Search Tree

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

INF2220: algorithms and data structures Series 1

Topic Binary Trees (Non-Linear Data Structures)

Multi-Way Search Tree

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CS 151. Binary Trees. Friday, October 5, 12

CS350: Data Structures Binary Search Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Tree Data Structures CSC 221

CS24 Week 8 Lecture 1

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

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

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

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Chapter 5. Binary Trees

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

Trees. CSE 373 Data Structures

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Garbage Collection: recycling unused memory

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Data Structures and Algorithms

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

CS102 Binary Search Trees

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

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

9. Binary Search Trees

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

Programming II (CS300)

Data Structures and Algorithms for Engineers

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

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

Data Structures and Algorithms

TREES Lecture 12 CS2110 Spring 2018

CS 350 : Data Structures Binary Search Trees

Binary Trees and Binary Search Trees

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

CE 221 Data Structures and Algorithms

Transcription:

Principles of Computer Science Binary Trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 1

Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary Tree Java Interfaces for Trees Operations on binary trees maketree() search( ) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 2

What is not great about using Arrays? Very difficult to maintain when the data is dynamic: How do you insert an element after the array is already sorted? How do you delete an element after the array is already sorted? Not easy (can be done using heaps). 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 3

Trees Tree data structures provide a natural way of managing hierarchical organizations 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 4

Hierarchical Organization Example: A university's organization A university's administrative structure. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 5

Tree Terminology A tree is A set of nodes Connected by edges The edges indicate relationships among nodes Nodes are arranged in levels Indicate the nodes' hierarchy Top level is a single node called the root 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 6

Tree Terminology Nodes at a given level are children of nodes at the previous level A node with children is the parent node of those children Nodes having the same parent are siblings A node with no children is a leaf node The only node with no parent is the root node All others have one parent each 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 7

Tree Terminology A node is reached from the root by a path The length of the path is the number of edges that compose it The height of a tree is the number of levels in the tree The subtree of a node is a tree rooted at a child of that node 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 8

Binary Trees Each node has at most two children Three binary trees. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 9

Why Binary Trees? Binary Trees: Support fast insert and delete Support fast sort Support fast search 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 10

Binary Trees A binary tree is either empty or has the following form T left and T right are themselves binary trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 11

Binary Trees Every nonleaf in a full binary tree has exactly two children A complete binary tree is full to its next-to-last level Leaves on last level are filled from left to right The height of a binary tree with n nodes that is either complete or full is log 2 (n + 1) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 12

Binary Trees The number of nodes in a full binary tree as a function of the tree's height. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 13

Binary Trees Full Tree Height Number of Nodes The number of nodes in a full binary tree as a function of the tree's height. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 14

Traversals of a Tree Visiting a node Processing the data within a node This is the action performed on each node during the traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree Visit the root Visit all nodes in the root's left subtree Visit all nodes in the root's right subtree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 15

Traversals of a Tree Preorder traversal: visit root before the subtrees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 16

Traversals of a Tree Inorder traversal: visit root between visiting the subtrees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 17

Traversals of a Tree Postorder traversal: visit root after visiting the subtrees These are examples of a depth-first traversal. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 18

Traversals of a Tree Level-order traversal: begin at the root, visit nodes one level at a time This is an example of a breadth-first traversal. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 19

Traversals of a General Tree A general tree has traversals that are Level order Preorder Postorder Inorder traversal is not well defined for a general tree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 20

Traversals of a General Tree (a) preorder. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 21

Traversals of a General Tree (b) postorder. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 22

Java Interfaces for Trees An interface that specifies operations common to all trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 23

Java Interfaces for Trees Interface of traversal methods for a tree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 24

Implement BinaryTree in Java class BinaryTreeNode<T> { public T content; public BinaryTreeNode<T> leftchild; public BinaryTreeNode<T> rightchild; } <lots of methods> class BinaryTree<T> { public BinaryTreeNode<T> root; } <lots of methods> 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 25

Binary Tree Consists of a root node. If a node is a leaf node if: leftchild = null, and rightchild = null. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 26

Binary Tree Traversal In Order class BinaryTree { BTNode root;... public void printinorder() { _printinorder(root); } private void _printinorder(btnode n) { if (n == null) return; } _printinorder(n.left); // print left subtree first System.out.println(n); // print the current node _printinorder(n.right); // print right subtree next }... 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 27

Binary Tree Traversal Pre Order class BinaryTree { BTNode root;... public void printpreorder() { _printpreorder(root); } private void _printpreorder(btnode n) { if (n == null) return; } System.out.println(n); // print the current node first _printpreorder(n.left); // print left subtree next _printpreorder(n.right); // print right subtree last }... 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 28

Binary Tree Traversal Post Order class BinaryTree { BTNode root;... public void printpostorder() { _printpostorder(root); } private void _printpostorder(btnode n) { if (n == null) return; } _printpostorder(n.left); // print left subtree first _printpostorder(n.right); // print right subtree second System.out.println(n); // print the current node last }... 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 29

Sorted Binary Tree A sorted binary tree is a binary tree with the property: for every node n, Left descendants n <= n < Right descendants of n 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 30

Sorted Binary Trees Sorted binary trees are sometimes called Binary Search Trees These provide log(n) search, insertion and deletion In practice if you think that you need to search through a binary tree, ensure that it is sorted 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 31

In Order Traversal of Sorted Binary Trees In order traversal of a sorted binary tree reaches nodes in increasing (sorted) order In order traversal 1 2 3 4 6 8 Sorted 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 32

Sorted Binary Tree - Insertions public void insertsorted(int value) { if (root == null) { // if this is the first value to be stored, root = new BTNode(value); // set it as the root return; } } _insertsorted(root, value); // else call the helper routine to store this // value at its appropriate location 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 33

Sorted Binary Tree - Insertions private void _insertsorted(btnode n, int value) { if (n.value >= value) { // (1) if value less than current node, go left if (n.left!= null) { // if left child exists, recur _insertsorted(n.left, value); } else { BTNode nn = new BTNode(value); // if nothing at left child, store value as left n.left = nn; // child of this node } } else { // (1) else go right if (n.right!= null) { // if right child exists, recur } } _insertsorted(n.right, value); } else { BTNode nn = new BTNode(value); // if nothing at right child, store value as n.right = nn; // right child of this node } 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 34

Binary Search Trees A search tree organizes its data so that a search is more efficient Binary search tree Nodes contain Comparable objects A node's data is greater than the data in the node's left subtree A node's data is less than the data in the node's right subtree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 35

Searching Through Sorted Binary Tree Given a sorted binary tree, we need to find the node that contains the value of interest: class BinaryTree { BinaryTreeNode<T> search(t value); } 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 36

Binary Search Through Sorted Tree class BinaryTree<T> {... public BinaryTreeNode<T> search(t value) { return searchatnode(value, this.root); } public BinaryTreeNode<T> searchatnode( T value, BinaryTreeNode<T> node) { if(node == null) return null; } } if(node.content == value) return node; else if(node.content > value) return searchatnode(value, node.leftchild); else return searchatnode(value, node.rightchild); 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 37

Sorted Binary Trees - Deletions Case 1: Deleting a leaf node parent node (parent.left) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 38

Sorted Binary Trees - Deletions Case 1: Deleting a leaf node This case is easy. Just remove the node. parent.left = null 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 39

Sorted Binary Trees - Deletions Case 2: Deleting a node with a single child (i.e., having only left or right subtree but not both) parent node (parent.left) child (node.right) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 40

Sorted Binary Trees - Deletions Case 2: Deleting a node with a single child (i.e., having only left or right subtree but not both) Replace the node with its child parent.left = node.left 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 41

Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) parent node (parent.left) in order predecessor of parent 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 42

Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) If the current node is the left child of the parent, replace node with the in order predecessor of parent node (parent.left) parent 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 43

Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) If the current node is the right child of the parent, replace node with the in order successor of parent 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 44

Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) Or simply replace the node with either its inorder successor or inorder predecessor 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 45

In Order Predecessor and Successor In order traversal 1 2 3 4 6 8 Sorted In the example above, 4 is in order predecessor of 6 and 8 is in order successor of 6. Simply check how these nodes are printed in in order predecessor 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 46

In Order Successor Input: node Output: succ Case 1: if node.left is not null, succ is the smallest element in the left subtree of node Case 2: otherwise, travel up using parent pointers until you reach a node that is the left child of its parent, the parent node is the succ Don t forget to take care of the special cases, e.g., what if the node has no succ? 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 47

In Order Predecessor Input: node Output: pred Case 1: if node.right is not null, pred is the smallest element in the left subtree of node Case 2: otherwise, travel up using parent pointers until you reach a node that is the right child of its parent, the parent node is the pred Don t forget to take care of the special cases, e.g., what if the node has no pred? 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 48

Summary Tree terminology Tree Traversals Binary Search Tree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 49

Readings Ch. 17 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 50