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

Similar documents
Binary Trees, Binary Search Trees

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

TREES. Trees - Introduction

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

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Lecture 6: Analysis of Algorithms (CS )

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

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

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

CSI33 Data Structures

Chapter 20: Binary Trees

CS350: Data Structures Tree Traversal

March 20/2003 Jayakanth Srinivasan,

Trees, Binary Trees, and Binary Search Trees

Trees. Truong Tuan Anh CSE-HCMUT

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Why Do We Need Trees?

Binary Trees. Recursive definition. Is this a binary tree?

Programming II (CS300)

Binary Trees

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

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

CE 221 Data Structures and Algorithms

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.


Lecture: Analysis of Algorithms (CS )

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Trees. Trees. CSE 2011 Winter 2007

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Associate Professor Dr. Raed Ibraheem Hamed

Principles of Computer Science

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

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

Trees, Part 1: Unbalanced Trees

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

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

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

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

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

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

Binary search trees :

Binary Trees. Examples:

Analysis of Algorithms

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

CSI33 Data Structures

Trees. CSE 373 Data Structures

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

Garbage Collection: recycling unused memory

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

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

TREES Lecture 10 CS2110 Spring2014

CS102 Binary Search Trees

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

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

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

Binary Trees Fall 2018 Margaret Reid-Miller

Data Structures and Algorithms for Engineers

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

Topic 14. The BinaryTree ADT

Data Structures. Dynamic Sets

Lecture 23: Binary Search Trees

Partha Sarathi Mandal

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

CMSC 341. Binary Search Trees CMSC 341 BST

Binary Search Trees. See Section 11.1 of the text.

Data Structure - Binary Tree 1 -

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms and Data Structures

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

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

CSCI2100B Data Structures Trees

Friday Four Square! 4:15PM, Outside Gates

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

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

12 Binary Search Tree

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

Successor/Predecessor Rules in Binary Trees

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

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

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

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

INF2220: algorithms and data structures Series 1

COSC 2011 Section N. Trees: Terminology and Basic Properties

Introduction to Binary Trees

tree nonlinear Examples

Stacks, Queues and Hierarchical Collections

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

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

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

BBM 201 Data structures

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees. Tree Structure Binary Tree Tree Traversals

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Algorithms. Deleting from Red-Black Trees B-Trees

CS 171: Introduction to Computer Science II. Binary Search Trees

TREES Lecture 12 CS2110 Spring 2018

Transcription:

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

Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms for Trees Binary Search Trees Searching, Insertion, and Deletion Analysis of Complexity CISC 235 Topic 3 2

Rooted Trees In a sequence, each element has zero or one predecessors & zero or one successors In a rooted tree, each element has either zero or one predecessor (the parent ) and zero or more successors (the children ) CISC 235 Topic 3 3

Tree Terminology Parent of x The node directly above node x in the tree Child of x A node directly below node x in the tree Siblings Nodes with common parent Root Only node with no parent Leaf or External Node A node with no children Internal Node Nonleaf node D B E A F CISC 235 Topic 3 4 G C H I

Tree Terminology Path A sequence of connected nodes Ancestor of x A node on the path from the root to x Descendent of x A node on a path from x to a leaf Empty Tree A tree with no nodes D B E A F G C H I CISC 235 Topic 3 5

Tree Terminology Height of Tree Number of edges on the longest path from the root to a leaf Depth of x Number of ancestors x has Level of x Number of edges on the path from the root to x (= depth) Degree of x Number of children x has D B E A F CISC 235 Topic 3 6 G C H I

Tree Properties The number of edges in a tree is one less than the number of nodes. B A C E = V - 1 D E F G H I CISC 235 Topic 3 7

Binary Trees: Recursive Definition A binary tree is one of: An empty tree A root with two binary trees as children (its left and right sub-trees) B G D A E H C F I CISC 235 Topic 3 8

Tree Implementations What would a linked representation of a binary tree look like? What would a linked representation of a general tree look like? What would an array representation of a binary tree look like? CISC 235 Topic 3 9

Representing a General Tree as a A Binary Tree B C D E F G H I A B C D E F G H I CISC 235 Topic 3 10

Array Implementation B A C A B C D E D E Node Location Root 1 Left Child of x 2 * location of x Right Child of x (2 * location of x) + 1 CISC 235 Topic 3 11

Traversal Sequences of Binary Trees Level Order Preorder A Root Inorder LT RT B C LT Root RT D E F Postorder LT RT Root G H I CISC 235 Topic 3 12

Inorder Traversal Algorithm Inorder-Tree-Walk ( x ) if x NIL then Inorder-Tree-Walk( left[x] ) print key[x] Inorder-Tree-Walk( right[x] ) CISC 235 Topic 3 13

Inorder Traversal Print Method void printinorder ( TreeNode root ) { if ( root!= null ) { printinorder( root.left ); System.out.println ( root.data ); printinorder( root.right ); } } CISC 235 Topic 3 14

Preorder Traversal for General Tree Preorder-Tree-Walk ( x ) print key[x] for each child w of x do Preorder-Tree-Walk( w ) A B C D E F G H CISC 235 Topic 3 15 I

Application: Expression Trees Infix customary form log a * (b + c) Prefix every operator before its operand(s) log * + * log a + b c Postfix every operator after its operand(s) a b c a log b c + * CISC 235 Topic 3 16

Recursive Thinking with Binary Assume you have the solution for both the left and right sub-tree (i.e., the recursive calls). Then figure out the solution for the whole tree. Trees CISC 235 Topic 3 17

Recursive Thinking with General Trees Assume you have the solution for all of the sub-trees (i.e., the recursive calls). Then figure out the solution for the whole tree. CISC 235 Topic 3 18

Practice Thinking Recursively 1. Write an algorithm to calculate the total number of nodes in a binary tree. 2. Write an algorithm to calculate the height of a binary tree. CISC 235 Topic 3 19

Application: File Systems 1. Print a list of all the files and folders in a hierarchical directory structure 2. Calculate the total number of blocks used by all files in the directory system. CISC 235 Topic 3 20

Print a Directory List (pseudocode) void listall( int depth ) { printname( depth ); // Print name of object if ( isdirectory( ) ) for each file c in this directory (each child) c.listall( depth + 1 ); } CISC 235 Topic 3 21

Calculate Total Size of All Files in a Directory (pseudocode) int size( ) { int totalsize = sizeofthisfile( ); if ( isdirectory( ) ) for each file c in this directory (each child) totalsize += c.size( ); } return totalsize; CISC 235 Topic 3 22

Binary Tree Properties Minimum # of nodes, minn, in a tree of height h: minn = Maximum height, maxh, for tree of n nodes: maxh = Maximum # of nodes, maxn, in a tree of height h: maxn = Minimum height, minh, in tree of n nodes: minh = CISC 235 Topic 3 23

Searching Methods Search: Given an integer x and a list L, presorted in increasing order, return the location of x in L, or -1 if x is not in L Algorithms: Linear Search: O(n) Binary Search: O( log n ) CISC 235 Topic 3 24

Comparison Tree Consider a list of names: Ali, Ania, Chi, Eva, Guy, Iori, Jen, Jui, Omar, Qi, Reni, Tess, Val, Zeya Jen Chi Reni Ali Guy Omar Val Ania Eva Iori Jui Qi Tess Zeya CISC 235 Topic 3 25

Binary Search Tree Property Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] key[x]. If y is a node in the right sub-tree of x, then key[x] key[y]. 6 6 2 8 2 8 1 4 1 4 9 3 Which is not a BST? Why not? 3 7 CISC 235 Topic 3 26

Binary Search Tree Algorithms Find, FindMin, FindMax 44 17 88 32 65 97 28 54 82 29 76 80 CISC 235 Topic 3 27

Recursive Search Algorithm // Returns node with key k in BST rooted // at node x, or NIL if not found Tree-Search( x, k ) if x = NIL or k = key[ x ] then return x if k < key[ x ] then return Tree-Search( left[ x ], k ) else return Tree-Search( right[ x ], k ) CISC 235 Topic 3 28

Iterative Search Algorithm // Returns node with key k in BST rooted // at node x, or NIL if not found Iterative-Tree-Search( x, k ) while x NIL and k key[ x ] do if k < key[ x ] then x left[ x ] else x right[ x ] return x CISC 235 Topic 3 29

Algorithm to Find Minimum // Returns minimum in BST // rooted at node x Tree-Minimum( x ) while left[ x ] NIL do x left[ x ] return x CISC 235 Topic 3 30

Algorithm to Find Maximum // Returns maximum in BST // rooted at node x Tree-Maximum( x ) while right[ x ] NIL do x right[ x ] return x CISC 235 Topic 3 31

Binary Search Tree: Insertion Insert 74 44 17 88 32 65 97 28 54 82 29 Search for the element as if it were in the tree. When reach a null node, that s where to insert. 74 76 New node 80 CISC 235 Topic 3 32

Recursive Insertion Algorithm for BST with no pointers to parents // Inserts node y into BST rooted at node root // Only for BSTs with no pointers to parents BSTInsert ( y, root ) if ( root = NIL ) // have found where to insert then root y else if key[ y ] < key[ root ] then left[root] = BSTInsert( y, left[ root ] ) else if key[ y ] > key[ root ] then right[root] = BSTInsert( y, right[ root ] ) else y is a duplicate; handle duplicate case return root CISC 235 Topic 3 33

Recursive Insert Function for BST with no pointers to parents Node insert ( int x, Node root ) { Node p = new Node( x ); if( root == null ) // empty tree root = p; else if ( x < root.data ) // goes in left sub-tree root.left = insert( x, root.left ); else if ( x > root.data ) // goes in right sub-tree root.right = insert( x, root.right ); else System.out.println( x + is already in tree ); return root; } CISC 235 Topic 3 34

Iterative Insertion Algorithm for BST with pointers to parents Tree-Insert ( T, z ) // Inserts node z into BST T y NIL x root[ T ] while x NIL // Find position at which to insert do y x if key[ z ] < key[ x ] then x left[ x ] else x right[ x ] p[ z ] y // Set parent link of new node if y = NIL // If tree T was empty then root[ T ] z // New node is root, else else if key[ z ] < key[ y ] // Connect node to parent then left[ y ] z else right[ y ] z CISC 235 Topic 3 35

BST: Find Successor of a Node Find Successor of 65 44 17 88 32 65 97 28 54 82 If right sub-tree of x is not empty, successor of x is leftmost node in its right sub-tree. 29 76 80 CISC 235 Topic 3 36

BST: Find Successor of a Node Find Successor of 32 44 17 88 32 65 97 28 54 82 29 If right sub-tree of x is empty, the successor of x is the lowest ancestor of x whose left child is also an ancestor of x 76 80 CISC 235 Topic 3 37

Find Successor Algorithm for BST with pointers to parents // Returns node in BST that is the successor // of node x, or NIL if no successor Tree-Successor( x ) if right[ x ] NIL then return Tree-Minimum( right[ x ] ) y p[ x ] while y NIL and x = right[ y ] do x y y p[ y ] return y CISC 235 Topic 3 38

Deletion of Node with Zero or One Delete 82 44 Child 17 88 32 65 97 28 54 82 29 76 Bypass the node to be deleted by setting its parent to point to its child 74 80 CISC 235 Topic 3 39

Deletion of Node with Two Children Delete 65 44 17 88 28 32 29 65 97 54 82 74 76 Step1: Overwrite the element of the node to be deleted with the minimum element of its right sub-tree 74 80 CISC 235 Topic 3 40

Deletion of Node with Two Children Delete 65 44 17 88 32 74 97 28 54 82 29 76 Step2: Recursively delete the node that contains the minimum element of its right sub-tree 74 80 Delete this node CISC 235 Topic 3 41

Iterative Deletion Algorithm for BST with pointers to parents // Deletes node z from BST T Tree-Delete ( T, z ) // Set y to point to node to splice out if left[ z ] = NIL or right[ z ] = NIL then y z else y Tree-Successor( z ) // Set x to non-nil child of y, // or to NIL if y has no children if left[ y ] NIL then x left[ y ] else x right[ y ] CISC 235 Topic 3 42

Deletion Algorithm, con. // Splice out node y (next 7 lines) if x NIL then p[ x ] p[ y ] if p[ y ] = NIL then root[ T ] x else if y = left[ p[ y ] ] then left[ p[ y ] ] x else right[ p[ y ] ] x // If successor to z was spliced out, copy y s data to z if y z then key[ z ] key[ y ] return y CISC 235 Topic 3 43

Binary Search Tree: Complexity Search, insertion, and deletion in a binary search tree are all O(h), where h is the height of the tree. What does this imply about the complexity in terms of n? CISC 235 Topic 3 44