Why Do We Need Trees?

Similar documents
Trees. CSE 373 Data Structures

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

Binary Trees, Binary Search Trees

Binary Trees

CSCI2100B Data Structures Trees

Chapter 20: Binary Trees

Trees. Trees. CSE 2011 Winter 2007

CE 221 Data Structures and Algorithms

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

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

Programming II (CS300)

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

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

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

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

COMP : Trees. COMP20012 Trees 219

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

Trees. A tree is a directed graph with the property

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

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

CSC148 Week 6. Larry Zhang

CSI33 Data Structures

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Principles of Computer Science

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

Cpt S 122 Data Structures. Data Structures Trees

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

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

Trees, Part 1: Unbalanced Trees

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

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

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

Trees. Truong Tuan Anh CSE-HCMUT

BBM 201 Data structures

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

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

Trees, Binary Trees, and Binary Search Trees

Recall: Properties of B-Trees

Topic 14. The BinaryTree ADT

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

CSE 214 Computer Science II Introduction to Tree

TREES. Trees - Introduction

TREES Lecture 10 CS2110 Spring2014

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

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

csci 210: Data Structures Trees

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


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

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

Binary Search Trees. See Section 11.1 of the text.

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

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

Unit III - Tree TREES

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

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

INF2220: algorithms and data structures Series 1

CS350: Data Structures Tree Traversal

COSC 2011 Section N. Trees: Terminology and Basic Properties

Trees. Eric McCreath

CS24 Week 8 Lecture 1

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Data Structures and Algorithms for Engineers

Analysis of Algorithms

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

Binary Trees. Height 1

tree nonlinear Examples

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

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

Algorithms and Data Structures

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

CSI33 Data Structures

Binary Trees. Examples:

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

Garbage Collection: recycling unused memory

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

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

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

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Trees Algorhyme by Radia Perlman

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Trees. Tree Structure Binary Tree Tree Traversals

Data Structure - Binary Tree 1 -

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

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

Course Review. Cpt S 223 Fall 2009

March 20/2003 Jayakanth Srinivasan,

CMPT 225. Binary Search Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

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

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Transcription:

CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees? Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed as a linear ordering File directories or folders on your computer Moves in a game Employee hierarchies in organizations and companies Family trees Classification hierarchies (e.g. phylum, family, genus, species) 1 2 Tree Jargon More Tree Jargon Basic terminology: nodes and edges root subtrees parent children, siblings leaves path ancestors descendants path length A B C D E Note: Arrows denote directed edges Trees always contain directed edges but arrows are often omitted. F 3 Length of a path = number of edges Depth of a node N = length of path from root to N Height of node N = length of longest path from N to a leaf Depth and height of tree =? depth=0, height = 2 A B C D depth = 2, height=0 E F 4

Definition and Tree Trivia Recursive Definition of a Tree: Atreeisasetofnodesthatis a. an empty set of nodes, or b. has one node called the root from which zero or more trees (subtrees) descend. A tree with N nodes always has edges Twonodesinatreehaveatmosthowmanypathsbetween them? Can a non-zero path from node N reach node N again? Does depth of nodes in a non-zero path increase or decrease? Definition and Tree Trivia Recursive Definition of a Tree: Atreeisasetofnodesthatis a. an empty set of nodes, or b. has one node called the root from which zero or more trees (subtrees) descend. A tree with N nodes always has N-1 edges Twonodesinatreehaveatmostonepathbetweenthem Can a non-zero path from node N reach node N again? No! Trees can never have cycles. Does depth of nodes in a non-zero path increase or decrease? Depth always increases in a non-zero path 5 6 Implementation of Trees Application: Arithmetic Expression Trees Obvious Pointer-Based Implementation: Node with value and pointers to children Problem: Do not usually know number of children for a node in advance. How many pointers should we allocate space for? Better Implementation: 1 st Child/Next Sibling Representation Each node has 2 pointers: one to its first child and one to next sibling Can handle arbitrary number of children A Exercise: Draw the representation for this tree B C D Example Arithmetic Expression: A+(B*(C/D)) How would you express this as a tree? E F 7 8

Application: Arithmetic Expression Trees Traversing Trees Example Arithmetic Expression: A+(B*(C/D)) Tree for the above expression: Used in most compilers No parenthesis need use tree structure Can speed up calculations e.g. replace / node with C/D if C and D are known Calculate by traversing tree (how?) + A * B / C D Preorder: Root, then Children +A*B/CD Postorder: Children, then Root ABCD/*+ Inorder: Left child, Root, Right child A+B*C/D + A * B / C D 9 Example Code for Recursive Preorder Preorder Traversal with a Stack void print_preorder ( TreeNode T) { TreeNode P; if ( T == NULL ) return; else { print_element(t-> Element); P = T -> FirstChild; while (P!= NULL) { print_preorder ( P ); P = P -> NextSibling; void Stack_Preorder (TreeNode T, Stack S) { if (T == NULL) return; else push(t,s); while (!isempty(s)) { T = pop(s); print_element(t -> Element); if (T -> Right!= NULL) push(t -> Right, S); if (T -> Left!= NULL) push(t -> Left, S); What is the running time for a tree with N nodes? What is the running time for a tree with N nodes? 12

Binary Trees Every node has at most two children Most popular tree in computer science Given N nodes, what is the minimum depth of a binary tree? What is the maximum depth of a binary tree? Binary Trees Every node has at most two children Most popular tree in computer science Given N nodes, what is the minimum depth of a binary tree? At depth d, you can have N = 2 d to 2 d+1-1 nodes (a full tree) So, minimum depth d is: log N d log(n+1)-1 or Θ(log N) What is the maximum depth of a binary tree? Degenerate case: Tree is a linked list! Maximum depth = N-1 Goal: Would like to keep depth at around log N to get better performance than linked list for operations like Find. 13 14 Binary Search Trees Operations on Binary Search Trees Binary search trees are binary trees in which the value in every node is: > all values in the node s left subtree 9 < all values in the node s right subtree Application: Look-up table Example: Given SSN, return student record SSN stored in each node as the key value 5 Operations: Find, FindMin, FindMax Insert, Delete How would you implement these? Recursive definition of binary search trees allows recursive routines! Position FindMin(Tree T) Position FindMax(Tree T) Position Find(ElementType X, Tree T) Tree Insert(ElementType X, Tree T) Tree Delete(ElementType X, Tree T) 5 9 15 16

Insert Operation Insert Operation Tree Insert(ElementType X, Tree T) Do a Find operation for X If X is found! update duplicates counter Else, Find stops at a NULL pointer Insert Node with X there! Tree Insert(ElementType X, Tree T) Do a Find operation for X If X is found! update duplicates counter Else, Find stops at a NULL pointer Insert Node with X there! Example: Insert 95 Example: Insert 95? 95 18 Delete Operation Delete Operation Delete is a bit trickier Why? Suppose you want to delete Strategy: Find Delete the node containing Problem: When you delete a node, what do you replace it by? Problem: When you delete a node, what do you replace it by? Solution: 1. If it has no children, by NULL 2. If it has 1 child, by that child 3. If it has 2 children, by the node with the smallest value in its right subtree Examples: 1. Delete 5 2. Delete 24 (note: recursive deletion) 3. Delete (note: recursive deletion) 19 20

Example: Delete Example: Delete Find, Replace with smallest value in right subtree Delete smallest value in right subtree 21 Delete in right subtree (recursive delete) Find 1child,so replace by child NULL Delete No child, so replace by NULL 22 Next Class: Analysis of Binary Search Tree Operations Other Species of Trees: AVL, splay, and B-trees Homework #2 will be assigned To Do: Read Chapter 4 23