Trees. See Chapter 18 of Weiss

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

Binary Trees

Programming II (CS300)

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

Checkout BinaryTrees from SVN. Java Collections Framework Intro to Trees

Data Structures and Algorithms

Principles of Computer Science

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

CSE 214 Computer Science II Introduction to Tree

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

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

CSC148 Week 6. Larry Zhang

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

CS F-11 B-Trees 1

Java Collections Framework Intro to Trees

TREES. Trees - Introduction

CS24 Week 8 Lecture 1

Chapter 20: Binary Trees

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

Friday Four Square! 4:15PM, Outside Gates

CS 206 Introduction to Computer Science II

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

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 =

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

Why Do We Need Trees?

Tree traversals and binary trees

Binary Trees, Binary Search Trees

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

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

Trees. Tree Structure Binary Tree Tree Traversals

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

Data Structures and Algorithms

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

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

CSE 100 Advanced Data Structures

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

BBM 201 Data structures

Organizing Spatial Data

CS302 - Data Structures using C++

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

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CE 221 Data Structures and Algorithms

Data Structures and Organization (p.5 Recursion. Binary Trees)

Trees. Eric McCreath

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

INF2220: algorithms and data structures Series 1

Data Structures - Binary Trees 1

Trees. CSE 373 Data Structures

Topic 14. The BinaryTree ADT

Binary Heaps in Dynamic Arrays

B-Trees. CS321 Spring 2014 Steve Cutchin

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

CIS265/ Trees Red-Black Trees. Some of the following material is from:

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

CSC148H Week 8. Sadia Sharmin. July 12, /29

Trees. Trees. CSE 2011 Winter 2007

Binary Trees Fall 2018 Margaret Reid-Miller

Hamilton paths & circuits. Gray codes. Hamilton Circuits. Planar Graphs. Hamilton circuits. 10 Nov 2015

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

Linked Structures Songs, Games, Movies Part III. Fall 2013 Carola Wenk

birds fly S NP N VP V Graphs and trees

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

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Binary Trees: Practice Problems

Lecture 26. Introduction to Trees. Trees

Data Structures in Java

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

Building Java Programs

Recursive Data Structures and Grammars

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25


Programming II (CS300)

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

Recursion and Structural Induction

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

Self-Balancing Search Trees. Chapter 11

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Maps; Binary Search Trees

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

Data and File Structures Laboratory

Binary Search Trees, etc.

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Lower Bound on Comparison-based Sorting

Data Structures and Algorithms for Engineers

Binary Trees and Binary Search Trees

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

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

CS 106X Lecture 18: Trees

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

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

IX. Binary Trees (Chapter 10)

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

Laboratory Module Trees

CS 206 Introduction to Computer Science II

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

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

EE 368. Weeks 5 (Notes)

Transcription:

Trees See Chapter 18 of Weiss

By the way, I am Bob Geitz www.cs.oberlin.edu/~bob/cs151 Ben is at a conference in Kansas City (where, according to Rogers and Hammerstein, everything is up to date and they've gone about as far as they can go...). He'll be back on Monday.

Trees are the second-most important data structure in programming, following only lists. We will think about trees in two different ways.

Graph definition: A tree is a kind of directed graph, so it has a set of nodes and a set of edges connecting the nodes. There is one special node called the root. Each node except for the root in a tree has an incoming edge from one other node. The root has no incoming edges. There is a path of edges from the root to every other node. Nodes that have no outgoing edges are called leaves.

We call the node at the start of an edge a parent node. The node at the end of this edge is the parent's child. In this terminology a node can have multiple children, and every node but the root has exactly one parent.

We measure the length of a path in the tree by the number of edges it contains (not the number of nodes). The height of a node is the longest path from it to a leaf. The height of the overall tree is the height of its root. The depth of a node is the length of the path from it to the root. The depth of the root itself is 0. A Node Height Depth A 3 0 B C B 1 1 C 2 1 D E F D 0 2 E 0 2 F 1 2 G G 0 3

Here is a recursive definition of a tree: A tree is either empty or it is a root r and 0 or more non-empty subtrees connected to r by an edge.

A binary tree is one in which each node can have at most two children. The children are often referred to as the left and right children.

Trees are used in many situations: Anything hierarchical, like file systems or administrative structures or Java class structures can be represented by trees. My Documents Classes Committees Personal CS 150 CS 151 CS 357

Many compilers build a tree representation of the program they are compiling, guided by a grammar for the programming language. The next slide shows a tree representing int x; x = 1; while (x < 10) { print(x); x = x + 1; }

program x int compound statement assign while x 1 < x 10 compound statement call assign print x x + x 1

Games are often represented by trees, where the root represents the current state of the game and children represent possible moves.

Indexes in a database are built on tree structures.

Since arithmetic operators take two arguments, one use of binary trees is in representing expressions. You might represent 3*(4+5) as * 3 + 4 5 Such a tree has operators in the interior nodes and numbers in the leaves. There is an easy recursive algorithm to compute its value -- evaluate the left child, evaluate the right child and apply the operator to those values.

So how do we represent trees? You can use arrays. If there are at most 2 children of each node, you can put the root at index 0, its children at index 1 and 2, the children of the node at index 1 could be at indices 3 and 4, and so forth. The children of the node at index n are at index 2n+1 and 2n+2. The parent of node at index k is at index (k-1)/2. So the array [2 9 1 3 5] represents the tree 2 9 1 3 5 If a node could have 3 children the kids of node [n] would be at [3n+1] [3n+2] and [3n+3]

A more flexible scheme is to use a linked structure. In Lab 5 we will use the following 3 classes: abstract class BinaryTree<T> { // methods we want the tree classes to have } class EmptyTree<T> extends BinaryTree<T> { // no data and just trivial methods } class ConsTree<T> textends BinaryTree<T> { T data; BinaryTree<T> left; BinaryTree<T> right; // non-trivial methods }

For example, suppose you want to add a height( ) method to the BinaryTree class. You do this in three steps:

First, you need to add an abstract height( ) method to the abstract BinaryTree<T> class: public abstract int height( );

Next, add a height( ) method to the EmptyTree<T> class. The EmptyTree methods are almost always trivial. In this case public int height( ) { return -1; }

Finally, add a height method to the ConsTree<T> class. This is usually the only step that has any substance. Because the BinaryTree structure is recursive, the ConsTree methods are usually recursive. In this case we want to find the height of a tree in terms of the heights of its children. G A B C D E F

The method for the ConsTree<T> class is int height( ) { return 1+ Math.max(left.height(), right.height() ); } Note that we defined the height of an empty tree as -1 to make this work. A leaf node has height 0 and 2 empty trees as children, so the empty trees need to have height -1.

Note also that our recursive tree structure has empty trees as the base cases at the bottom of our trees. ConsTrees always have children, so every downward path in the tree is terminated by an EmptyTree. This means that the base cases for our recursions are the methods in the EmptyTree class.