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

Similar documents
Binary Trees

CSE 214 Computer Science II Introduction to Tree

Programming II (CS300)

Binary Trees, Binary Search Trees

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

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

Data and File Structures Laboratory

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

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Trees. Truong Tuan Anh CSE-HCMUT

Data Structures and Algorithms

CE 221 Data Structures and Algorithms

Data Structure - Binary Tree 1 -

Binary Trees. Height 1

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

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

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

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

Principles of Computer Science

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

TREES. Trees - Introduction

BBM 201 Data structures

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

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

CSC148 Week 6. Larry Zhang

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

Binary Trees. Examples:

Trees. CSE 373 Data Structures

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

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

Introduction to Computers and Programming. Concept Question

Organizing Spatial Data

Algorithms. Deleting from Red-Black Trees B-Trees

Garbage Collection: recycling unused memory

CS F-11 B-Trees 1

Data Structures and Algorithms

EE 368. Weeks 5 (Notes)

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

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

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

Why Do We Need Trees?

Trees. Carlos Moreno uwaterloo.ca EIT

Lecture 6: Analysis of Algorithms (CS )

Trees. Trees. CSE 2011 Winter 2007

TREES Lecture 10 CS2110 Spring2014

Trees and Binary Trees

Advanced Set Representation Methods

Programming II (CS300)

CMPT 225. Binary Search Trees

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

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

Design and Analysis of Algorithms Lecture- 9: B- Trees

Motivation for B-Trees

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

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

Algorithms and Data Structures

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

CS301 - Data Structures Glossary By

11 TREES DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

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

Algorithms. AVL Tree

CSCI2100B Data Structures Trees

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

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

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

2.2 Syntax Definition

Recall: Properties of B-Trees


Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

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

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

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

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

CS24 Week 8 Lecture 1

Laboratory Module Trees

[ DATA STRUCTURES ] Fig. (1) : A Tree

ITI Introduction to Computing II

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Trees, Binary Trees, and Binary Search Trees

Data Structure. IBPS SO (IT- Officer) Exam 2017

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

Trees. Eric McCreath

13.4 Deletion in red-black trees

The questions will be short answer, similar to the problems you have done on the homework

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

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

m-way Search Tree: Empty, or if not empty then: Each internal node has q children and q 1 elements, for 2 q m.

ITI Introduction to Computing II

13.4 Deletion in red-black trees

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

TREES Lecture 12 CS2110 Spring 2019

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Data Structures. Trees, Binary trees & Binary Search Trees

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Priority Queues and Binary Heaps

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

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

8. Binary Search Tree

Transcription:

Lecture 32 No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 1

Outline Introduction to trees Tree terminology Binary trees Binary tree representations Array Linked nodes Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 2

Introduction to Trees So far, all containers that we have looked at are sequential and access is by position (either index or iterator). Many applications access data by value rather than by position. For example, a phone book entry is accessed by a person's name. Could keep entries in a list and scan the list for the name. As we have seen scanning a list is a slow O(n) operation. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 3

Introduction to Trees Containers that are accessed by value are called associative containers. Unfortunately, we will not get to the STL associative containers, map, set, and hashmap, in this class. However, we will look at the tree data structure that is often used to implement associative containers. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 4

Tree Terminology A tree is a hierarchical data structure. It places elements in nodes connect by branches that originate from a single root. For example, the organizational structure of the academic programs at UE. UE root node SOBA CEHS CECS CAS ACCT & BA NHS SED EXSS EECS MCE FL MATH NURS PT EE CoE CS SPAN FREN Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 5

Tree Terminology The root node is the top level and has no predecessor (or parent). It can have multiple successors (called children who are siblings to each other) to the next level. Each node contains a value and a set of 0 or more links to children nodes. The links are called edges. A node with no children is called a leaf. All other non-root, non-leaf nodes are interior nodes with at least one child. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 6

Tree Terminology Every node is the root of a subtree the node and all its descendants. (Its children and the children's children, etc.) A node's parent and the parent's parent, etc. are the node's ancestors. A path is a sequence of nodes from a node (N) to the root (R): N = X 0 X 1...X k = R where k is the length of the path. Each node X i+1 is the parent of X i. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 7

Tree Terminology The length of a path defines the depth of the node. The depth of a tree is the maximum depth of any node in the tree. Sometimes the (depth of a tree +1) is called the height of the tree (though the textbook says that the height of a tree is the same as the depth of a tree). Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 8

Tree Terminology Here is the UE organization chart again, annotated with some of the tree terminology. subtree rooted at SOBA UE root; parent of CECS; ancestor to all SOBA CEHS interior node CECS CAS ACCT & BA NHS SED EXSS EECS MCE FL MATH NURS PT EE CoE CS SPAN FREN children of NHS; siblings to each other leaf; child of EECS; descendant of CECS Path from CS: CS->EECS->CECS->UE; CS at depth 3 Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 9

Binary Trees Organization charts are one of a few applications that need trees where the nodes have more than 2 children. A file system with its directories would be another. Most applications only need a binary tree, a tree where each node has at most 2 children. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 10

Binary Trees Formally, a binary tree is a finite set of nodes. The set of nodes may be empty, called an empty tree. If the set is not empty, it meets the following rules: 1. There is one special node called the root. 2. Each node may be associated with up to two other different nodes, called its left child and its right child. 3. Each node, except the root, has exactly one parent; the root has no parent. 4. There is a path from every node following its parent back to the root. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 11

Binary Trees A binary tree is said to be full, if every leaf has the same depth and every node has two children. A binary tree is said to be complete, if every level except the deepest level is full and the nodes in the deepest level are as far left as possible. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 12

Binary Trees Here are some binary trees: full binary tree complete binary tree binary tree that is neither full nor complete Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 13

Array Representation When a binary tree is complete, we can use a simple array representation (a fixed-size array if the tree has a maximum size or a dynamic array if can grow indefinitely). Suppose we number the nodes starting at the root and going from left to right at each level and then top to bottom. Call this number i. Then we store each node's value in an array at index i. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 14

Array Representation Here is a picture: 'A' 0 'L' 1 'G' 2 'O' 'R' 'I' 'T' 3 4 5 6 'H' 'M' 'S' 7 8 9 array 'A' 'L' 'G' 'O' 'R' 'I' 'T' 'H' 'M' 'S' [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 15

Array Representation This representation is convenient for several reasons: The root is always at array[0] Suppose that the data for a node appears in array[i]. The locations of the parent and children node can be computed. For a non-root node, the parent is always located at array[(i-1)/2] (using integer division) The children (if they exist) are located at array[2i+1] (left child) and array[2i+2] (right child) Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 16

Array Representation The main problem with the array representation is that if the tree is not complete, then there must be a way to indicate which elements of the array actually exist. The representation also can be very inefficient if the tree is very deep with few nodes at each level. The array would have to be very large, but would be mostly empty. Solve these problems by implementing nodes and edges directly. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 17

Linked Node Representation We can represent a tree node using a class that has an attribute to hold the node value (data) and two tree node pointer attributes (leftchild and rightchild). The pointer attributes are used to link a node to the nodes of its children. An entire tree is used represented as a pointer to the root node. The empty tree is represented using the null pointer. A partial picture of the previous example tree is shown on the next slide. Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 18

Linked Node Representation root 'A' data leftchild rightchild 'L' 'G' 'O' 'R' 'I' 'T' Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 19