Binary Trees Fall 2018 Margaret Reid-Miller

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

TREES. Trees - Introduction

Binary Trees, Binary Search Trees

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

Binary Trees

Programming II (CS300)

Chapter 20: Binary Trees

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

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

March 20/2003 Jayakanth Srinivasan,

IMPLEMENTING BINARY TREES

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

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

CS 206 Introduction to Computer Science II

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

CS 206 Introduction to Computer Science II

Trees. Truong Tuan Anh CSE-HCMUT

CE 221 Data Structures and Algorithms

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

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. Tree Structure Binary Tree Tree Traversals

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

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

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

BBM 201 Data structures

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

Principles of Computer Science

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

Trees. CSE 373 Data Structures

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

Binary trees. Binary trees. Binary trees

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

Introduction to Computers and Programming. Concept Question

CS350: Data Structures Tree Traversal

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

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

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

Data Structures and Algorithms for Engineers

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

Trees, Binary Trees, and Binary Search Trees

Trees. Trees. CSE 2011 Winter 2007

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

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

Garbage Collection: recycling unused memory

Topic 14. The BinaryTree ADT

CSC148 Week 6. Larry Zhang

Algorithms and Data Structures

Lecture 23: Binary Search Trees

INF2220: algorithms and data structures Series 1

CSI33 Data Structures

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

CMPT 225. Binary Search Trees

tree nonlinear Examples

Why Do We Need 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

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

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

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

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

IX. Binary Trees (Chapter 10)

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


Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

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

Binary Trees and Huffman Encoding Binary Search Trees

CSC/MAT-220: Lab 6. Due: 11/26/2018

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

Data Structures and Algorithms

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms. Deleting from Red-Black Trees B-Trees

Lecture 26. Introduction to Trees. Trees

If you took your exam home last time, I will still regrade it if you want.

Trees. Eric McCreath

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

TREES Lecture 10 CS2110 Spring2014

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

CS302 - Data Structures using C++

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

CS 206 Introduction to Computer Science II

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

CSI33 Data Structures

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

Computer Science II Fall 2009

Friday Four Square! 4:15PM, Outside Gates

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

Successor/Predecessor Rules in Binary Trees

Trees Algorhyme by Radia Perlman

LECTURE 13 BINARY TREES

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

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

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

CSC148-Section:L0301

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

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

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

Self-Balancing Search Trees. Chapter 11

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Transcription:

Binary Trees 15-121 Fall 2018 Margaret Reid-Miller

Trees Fall 2018 15-121 (Reid-Miller) 2

Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also binary trees. A binary tree is a nonlinear data structure. The top node of a tree is called the root. Any node in a binary tree has at most 2 children. Any node in a binary tree has exactly one parent node (except the root). Fall 2018 15-121 (Reid-Miller) 3

Tree Terminology A root B C internal leaf D E F G Fall 2018 15-121 (Reid-Miller) 4

left-child Tree Terminology A parent B C right-child siblings D E F G Fall 2018 15-121 (Reid-Miller) 5

Tree Terminology left-subtree A root right-subtree B C D E F G Fall 2018 15-121 (Reid-Miller) 6

Types of Binary Trees Expression Trees * + - / 5 7 3 6 2 (6 / 2 + 5) * (7-3) Fall 2018 15-121 (Reid-Miller) 7

Types of Binary Trees Huffman Trees coding A 45% B 30% C 20% D 5% 0 B 0 y 0 C z 1 x 1 A 1 D A 1 B 00 C 010 D 011 1001010100 = ABACAB Fall 2018 15-121 (Reid-Miller) 8

Types of Binary Trees Binary Search Trees 84 24 41 96 50 98 13 37 Fall 2018 15-121 (Reid-Miller) 9

More Terminology A perfect binary tree is a binary tree such that - all leaves have the same level, and - every non-leaf node has 2 children. A full tree is a binary tree such that - every non-leaf node has 2 children. A complete binary tree is a binary tree such that - every level of the tree has the maximum number of nodes possible except possibly the deepest level, and - at the deepest level, the nodes are as far left as possible. Fall 2018 15-121 (Reid-Miller) 10

Examples A A B C B C D E F G D E F PERFECT (and COMPLETE) COMPLETE (but not FULL) Fall 2018 15-121 (Reid-Miller) 11

Binary Trees & Recursion Consider two nodes in a tree, X and Y. X is an ancestor of Y if X is the parent of Y, or X is the ancestor of the parent of Y. It s RECURSIVE! X is a descendant of Y if X is the child of Y, or X is the descendant of the child of Y. Fall 2018 15-121 (Reid-Miller) 12

Binary Trees Levels A Level 0 B C Level 1 D E F G Level 2 Fall 2018 15-121 (Reid-Miller) 13

Binary Trees - Levels The level of a node Y is BASE CASE 0, if the Y is the root RECURSIVE CASE 1 + the level of the parent of Y, if Y is not the root Fall 2018 15-121 (Reid-Miller) 14

Binary Tree - Height The height of a binary tree T is the number of nodes in the longest path from the root node to a leaf node. BASE CASE 0, if T is empty RECURSIVE CASE max(height(left(t)), height(right(t))) + 1, if T is not empty Fall 2018 15-121 (Reid-Miller) 15

Binary Tree Traversals PREORDER INORDER POSTORDER A ABDECFG DBEAFCG DEBFGCA B C D E F G Fall 2018 15-121 (Reid-Miller) 16

Traversal Example B F D A G E C H preorder ABDFGCEHI inorder BFDGAEIHC postorder FGDBIHECA I Fall 2018 15-121 (Reid-Miller) 17

Traversals are Recursive Preorder traversal 1. Visit the root. 2. Perform a preorder traversal of the left subtree. 3. Perform a preorder traversal of the right subtree. Inorder traversal 1. Perform an inorder traversal of the left subtree. 2. Visit the root. 3. Perform an inorder traversal of the right subtree. Postorder traversal 1. Perform a postorder traversal of the left subtree. 2. Perform a postorder traversal of the right subtree. 3. Visit the root. Fall 2018 15-121 (Reid-Miller) 18

Traversals on Expression Trees What do you get when you perform a postorder traversal on an expression tree? + - * / 5 7 3 6 2 6 2 / 5 + 7 3 - * Fall 2018 15-121 (Reid-Miller) 19

Traversals on Binary Search Trees What do you get when you perform an INORDER traversal on a binary search tree? 84 41 96 24 50 98 13 37 13 24 37 41 50 84 96 98 Fall 2018 15-121 (Reid-Miller) 20

Implementing a binary tree Use an array to store the nodes. - mainly useful for complete binary trees (more on this soon) Use a variant of a linked list where each data element is stored in a node with links to the left and right children of that node Instead of a head reference, we will use a root reference to the root node of the tree. Fall 2018 15-121 (Reid-Miller) 21

Binary Tree Node (BTNode inner class) private class BTNode { private E data; private BTNode left; private BTNode right; } public BTNode(E element) { data = element; left = null; right = null; } data Fall 2018 15-121 (Reid-Miller) 22

BinaryTree class (Constructor) public class BinaryTree<E> { private BTNode root; public BinaryTree() { } root = null; Fall 2018 15-121 (Reid-Miller) 23

BinaryTree class (another constructor) public BinaryTree(E element, BinaryTree<E> lefttree, BinaryTree<E> righttree) { } root = new BTNode(element); if (lefttree!= null) root.left = lefttree.root; if (righttree!= null) root.right = righttree.root; Fall 2018 15-121 (Reid-Miller) 24

BinaryTree class short-circuit evaluation public BinaryTree<E> getleftsubtree() { } if (root!= null && root.left!= null) return new BinaryTree<E>(root.left); else return null; protected BinaryTree(BTNode rootref) { root = rootref; special constructor for this method only } same idea for right subtree Fall 2018 15-121 (Reid-Miller) 25

Preorder Traversal public String preorder() { return preorder(root); } private String preorder(node<e> node) { String result = ""; if (node!= null) { result += node.data + " "; result += preorder(node.left) + " "; result += preorder(node.right) + " "; } return result; } Fall 2018 15-121 (Reid-Miller) 26