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

Similar documents
Trees. See Chapter 18 of Weiss

Binary Trees

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

CS 151. Binary Search Trees. Wednesday, October 10, 12

Programming II (CS300)


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

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

CS24 Week 8 Lecture 1

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

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

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

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

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

CSE 214 Computer Science II Introduction to Tree

Trees Algorhyme by Radia Perlman

Principles of Computer Science

Trees. CSE 373 Data Structures

Binary Trees, Binary Search Trees

March 20/2003 Jayakanth Srinivasan,

Trees. Truong Tuan Anh CSE-HCMUT

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

CE 221 Data Structures and Algorithms

Name CPTR246 Spring '17 (100 total points) Exam 3

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

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

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

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

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

CS302 - Data Structures using C++

Trees. Eric McCreath

Binary Trees: Practice Problems

ITI Introduction to Computing II

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

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Graph and Digraph Glossary

ITI Introduction to Computing II

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

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

Programming II (CS300)

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

Binary Trees Fall 2018 Margaret Reid-Miller

Chapter 20: Binary Trees

CS 206 Introduction to Computer Science II

CS 310 Advanced Data Structures and Algorithms

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

tree nonlinear Examples

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Trees. Tree Structure Binary Tree Tree Traversals

BBM 201 Data structures

Why Do We Need Trees?

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

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

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

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

Trees. Arash Rafiey. 20 October, 2015

Computer Science 136 Exam 2

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

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

Garbage Collection: recycling unused memory

CSC148 Week 6. Larry Zhang

Foundations of Discrete Mathematics

TREES. Trees - Introduction

Trees and Tree Traversal

Data Structures and Algorithms

TREES Lecture 12 CS2110 Spring 2018

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Programming II (CS300)

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Module 6: Binary Trees

COMP : Trees. COMP20012 Trees 219

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

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

Data Structures Brett Bernstein

Algorithms and Data Structures

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Definition of Graphs and Trees. Representation of Trees.

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

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

Introduction to Computers and Programming. Concept Question

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

TREES Lecture 12 CS2110 Fall 2016

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

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

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

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

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

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

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

Algorithms. Deleting from Red-Black Trees B-Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Algorithms and Data Structures CS-CO-412

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Topic 14. The BinaryTree ADT

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

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

Searching in Graphs (cut points)

Tree traversals and binary trees

Transcription:

CS 151 Binary Trees 1

Binary Tree Examples Without telling you what a binary tree is, here are some examples (that I will draw on the board): The dots/circles are called nodes, or vertices (singular: one vertex). The lines (that connect 2 nodes) are called edges, or sometimes arcs. A tree has a designated root node, typically drawn at the top of the tree. 2 2

Binary Tree Definition We can define a binary tree recursively, as follows: A binary tree is either en empty tree a root node r, connected by an edge to up to two non-empty trees Every node except the root has exactly one parent node (above it). Every node v has a unique path from the root to v. A tree has no cycles (i.e., loops). The number of edges in a tree is one less than the number of nodes. 3 3

Tree Vocabulary Trees come with a lot of special vocabulary that you should get familiar with! a leaf node is a node with no non-empty children an internal node is any node that is not a leaf node a (simple) path is a sequence of consecutive edges a cycle is a sequence of consecutive edges that starts and ends at the same vertex (a tree is acyclic, that is, it has no cycles.) the length of a path is the number of edges in that path fact: the number of nodes in a path = 1 + the number of edges in the path the depth, or level, of a node v is the length of the path from the root to v the height of a tree is the length of the longest path from the root to any node by this definition, the height of the single-node tree is 0 some people define height differently, so that height is one more than this. the degree of a node is the number of edges incident to it ancestor, descendant, sibling: are exactly what you expect... a forest is a collection of trees a directed tree is one where the edges are arrows from one node to the other in directed trees, a node has both in-degree and out-degree 4 4

Binary Tree Operations A binary tree stores data in its nodes, like a linked list. Some tree operations: size() - return the number of nodes in the tree isempty() - return whether this tree has any nodes isleaf() - return whether this tree is a single leaf node height() - return the height of the tree leafcount() - return the number of leaves in the tree nodecount() - return the number of nodes in the tree mirrorimage() - return a tree that is the mirror image of this tree traverse()/iterate() - go through the nodes of the tree in a specific order many, many others 5 5

Recursive Binary Tree Implementation Just liked the recursive LL implementation we ll have 3 separate public classes: /* This abstract class represents a Binary Tree. It defines the methods that every binary tree must have, but doesn t implement them. We will have different implementations depending on whether our tree is empty, or is a ConsTree, i.e. a root with left & right subtrees. */ public abstract class BinaryTree<T> { // method headers for Binary Tree methods go here } public class EmptyTree<T> extends BinaryTree<T> { // method implementations for empty trees go here } public class ConsTree<T> extends BinaryTree<T> { T data // the data of the root node BinaryTree<T> left // the left subtree BinaryTree<T> right // the right subtree // method implementations for non-empty trees go here } 6 6

Recursive Binary Tree Implementation To add a method to our BinaryTree class, we must do three things: add the method as an abstract method header in the BinaryTree class implement the method in the EmptyTree class implement the method in the ConsTree class For example, let s start with the isempty() method. We first add the method header to the BinaryTree class, to declare that any class that implements BinaryTree must have the isempty method. public abstract class BinaryTree<T> { public abstract boolean isempty(); In the EmptyTree class, we implement the method. public class EmptyTree<T> { public boolean isempty() { return true; } // always. In the ConsTree class, we implement the method. public class ConsTree<T> { public boolean isempty() { return false; } // always. 7 7

Recursive Binary Tree Implementation Now let s try the size() method that returns the number of nodes in the tree: We add the method header to the abstract LinkedList class: public abstract class BinaryTree<T> { public abstract int size(); We implement the method in the Empty class. public class EmptyTree<T> { public int size() { return 0; } // base case We implement size in the Cons class. What is the size of this cons list? public class ConsTree<T> { T data BinaryTree<T> left BinaryTree<T> right public int size() { return 1 + left.size() + right.size() } 8 8

Recursive Binary Tree Implementation Now let s try the height() method, which is the length of the longest root-leaf path in the tree. public abstract class BinaryTree<T> { public abstract int height(); public class EmptyTree<T> { public int height() { return -1; } public class ConsTree<T> { T data BinaryTree<T> left BinaryTree<T> right public int size() { return 1 + Math.max( left.height(), right.height() ) } 9 9

Binary Tree Traversals There are many ways to traverse the tree such that each node is visited exactly once, in some order. The three steps for common tree traversals: 1. visit the current node 2. recursively traverse the left subtree 3. recursively traverse the right subtree Different orderings of these three steps leads to three different traversals! pre-order traversal: 1-2-3 (traverse the root node FIRST (pre), then do the recursive calls.) post-order traversal: 2-3-1 (do the recursive calls first, then traverse the root node (POST).) in-order traversal: 2-1-3 (traverse the root node IN between the two recursive calls.) 10 10

A couple of other important traversals are as follows: Binary Tree Traversals A breadth-first search (bfs) is a level-order traversal, where you visit the nodes in the tree level-by-level from the root down to the leaves. A depth-first search (dfs) starts at the root, visits nodes along one path until it hits a leaf, then backs up as little as possible before going down a different path to a leaf. (Hopefully this reminds you of backtracking, also, it is the same as preorder.) 11 11