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

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

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

Binary Trees

Programming II (CS300)

Binary Trees, Binary Search Trees

Chapter 20: Binary Trees

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

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

CE 221 Data Structures and Algorithms

TREES. Trees - Introduction

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

CS24 Week 8 Lecture 1

Data Structures - Binary Trees and Operations on Binary Trees

CS350: Data Structures Tree Traversal

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Why Do We Need Trees?

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

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

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Trees. CSE 373 Data Structures

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

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

Data Structures and Algorithms for Engineers

Trees. Truong Tuan Anh CSE-HCMUT

Data Structure Advanced

CSI33 Data Structures

March 20/2003 Jayakanth Srinivasan,

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

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

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

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.


Binary Trees. Height 1

Garbage Collection: recycling unused memory

Objectives. In this session, you will learn to:

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

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! Ellen Walker! CPSC 201 Data Structures! Hiram College!

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

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

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Trees. Tree Structure Binary Tree Tree Traversals

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

Algorithms and Data Structures

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

Binary Search Trees Treesort

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

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

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

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

Algorithms. Deleting from Red-Black Trees B-Trees

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

CSE 326: Data Structures Binary Search Trees

Trees. Trees. CSE 2011 Winter 2007

Introduction to Binary Trees

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

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

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

12 Abstract Data Types

INF2220: algorithms and data structures Series 1

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

Binary Trees. Examples:

BBM 201 Data structures

Friday Four Square! 4:15PM, Outside Gates

CS61B Lecture #21. A Recursive Structure. Fundamental Operation: Traversal. Preorder Traversal and Prefix Expressions. (- (- (* x (+ y 3))) z)

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs


Tree traversals and binary trees

Multi-Way Search Tree

Cpt S 122 Data Structures. Data Structures Trees

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

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

Self-Balancing Search Trees. Chapter 11

Trees: examples (Family trees)

Associate Professor Dr. Raed Ibraheem Hamed

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

COSC 2011 Section N. Trees: Terminology and Basic Properties

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

CSC148 Week 6. Larry Zhang

Unit III - Tree TREES

CMPT 225. Binary Search Trees

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

Binary Trees and Binary Search Trees

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CS 206 Introduction to Computer Science II

UNIT II/DATA SRUCTURES

CS301 - Data Structures Glossary By

Topic 14. The BinaryTree ADT

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

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

CSI33 Data Structures

Transcription:

Data Structures inary Trees number of leaves:?? height leaves Depth (Maximum level of the tree) leaves or nodes Root Level = 0 Level=1 57 feet root 2 Level=2 Number of nodes: 2 (2+1) - 1 = 7 2

inary Trees Levels - Nodes Max. number of leaves/nodes: 2 (d+1) - 1 where d = depth (assume the tree is full (complete)) The level of a node is its distance from the root. The depth of a tree is the level of the leaf in the longest path from the root plus one. strictly binary tree: every non-leaf node has nonempty left & right sub trees IS265/506: hapter 08 Intro to inary Trees 3 inary Trees Levels - Nodes complete binary tree of depth d has all leaves at level d n almost complete binary tree of depth d is such that: 1. ny node at a level less than (d-1) has 2 sons 2. The bottom level at height d is partially filled from left to right, and may only have from 1 to 2 d nodes. This may make some of the nodes at level d-1 into leaf nodes. So a binary tree of height d may have anywhere from 2 d to 2 d+1-1 nodes. tree can be strictly binary, but not almost complete onversely, a tree can be almost complete, but not strictly binary. IS265/506: hapter 08 Intro to inary Trees 4

inary Trees Levels Nodes tree can be strictly binary, but not almost complete onversely, a tree can be almost complete, but not strictly binary. IS265/506: hapter 08 Intro to inary Trees 5 inary Trees binary tree is a tree whose nodes have up to two children (possibly empty). Each circle represents a node. The circle right below it (and attached by the arrow) is the child (or leaf). Each child is either the left or right child. Each child can have only one parent, and each parent can have only two children. Each path (from node to node) is a straight line. There are no cycles in binary trees. IS265/506: hapter 08 Intro to inary Trees 6

inary Trees This is a tree, but not a binary tree. There are 2 nodes that have three children. IS265/506: hapter 08 Intro to inary Trees 7 re these binary trees? Why or why not?? IS265/506: hapter 08 Intro to inary Trees 8

inary Trees Why do care for binary trees? Improve speed when searching a long list Trees are naturally ordered - information is placed in a logical location so it can be found easier. 1 2 3 4 5 1 3 2 4 5 IS265/506: hapter 08 Intro to inary Trees 9 Implementing a inary Tree Trees can be implemented as either arrays or dynamic structures, similar to linked lists. We will only look at the dynamic implementation - the array implementation can be difficult to manage. left info right Left & right are pointers to nodes (children) in the tree. Info is where your data are stored. IS265/506: hapter 08 Intro to inary Trees 10

onceptual View of a Tree data *left *right data data *left *right *left *right data data data data *left *right *left *right *left *right *left *right IS265/506: hapter 08 Intro to inary Trees 11 inary Trees - Ordering Trees can be ordered in many ways. The most common are: Hierarchical: Highest to lowest. Example - university has multiple campuses, which have multiple colleges, which have many departments, etc. hronological: The order in the tree is determined by when the item was placed in the tree. Search Trees (lternately called ordered trees): formal definition: if a node n of the tree contains the value v, then all the values stored in the left subtree are less than v, and all the values in the right subtree are greater than v. Duplicate values are not allowed. IS265/506: hapter 08 Intro to inary Trees 12

Traversing a tree Three important ways to visit every node of a tree. INORDER (left, root, right) Inorder visits the left child, root, then right child PREORDER (root, left, right) Preorder visits the root, the left child, and then the right child POSTORDER (left, right, root) Postorder visits the left child, the right child, and finally the root. IS265/506: hapter 08 Intro to inary Trees 13 Tree Traversal The mechanics of visiting nodes could be accomplished by either: an explicitly navigation (I will do everything by myself approach), or using recursion (let the system help me). The explicit method requires you to define and use a stack - all data points are pushed on the stack as we visit them from top-to-bottom and/or left-to-right, and we then pop each element from the stack to view the data. This could be quite cumbersome. IS265/506: hapter 08 Intro to inary Trees 14

Sample Tree Structure Tree ( pointer to the root) Left Sub Tree Right Sub Tree D E Top-to-ottom (Depth-First Search) Left-to-Right (reath-first Search),, D, E,,,, D, E Examining One Node info tree tree.getleft() Tree.getRight() IS265/506: hapter 08 Intro to inary Trees 16

Tree Traversal n elegant visitation method is based on recursion. This requires a bit of extra effort on your part when debugging, but the code is much shorter and simpler to write. The algorithm for INORDER Traversals: ssume the tree exists Visit the left sub-tree Visit the root Visit the right sub-tree IS265/506: hapter 08 Intro to inary Trees 17 Let s Look at the ode // Inorder visits: // the left child, root, then right child void inordertraversal(nodeptr tree) { if (tree!= NULL) { inordertraversal(tree.getleft()); System.out.println(tree.getInfo()); inordertraversal(tree.getright()); } } IS265/506: hapter 08 Intro to inary Trees 18

Simple ase - Inorder blue red black Is the tree NULL? No, it does exist inordertraversal( tree.getleft ) - Visit the red () node: We call the procedure again using the red () node as the tree. There is no left node of red (), so we print the root. Now, there is no right node of red (), so we exit the recursive call. System.out.println( tree.getinfo ); - We print the blue () node. IS265/506: hapter 08 Intro to inary Trees 19 Simple ase (ontinued) blue red black inordertraversal( tree.getright ) - Visit the black () node: We call the procedure again using the black () node as the tree. There is no left node of black (), so we print the root. Now, there is no right node of black (), so we exit the recursive call. /* end if */ - We are done. Our output is: red () blue () black () IS265/506: hapter 08 Intro to inary Trees 20

Let s do one that is more difficult E D F D E F IS265/506: hapter 08 Intro to inary Trees 21 PreOrder Traversal Preorder visits the root, the left child, and then the right child The algorithm for PREORDER Traversals: ssume the tree exists Visit the root Visit the left subtree Visit the right subtree IS265/506: hapter 08 Intro to inary Trees 22

Let s Look at the ode // Preorder visits // the root, left child, then right child void preordertraversal(nodeptr tree) { if (tree!= NULL) { System.out.println( tree.getinfo() ); preordertraversal( tree.getleft() ); preordertraversal( tree.getright() ); } } IS265/506: hapter 08 Intro to inary Trees 23 Very Simple ase blue red black Is the tree NULL? No, it does exist System.out.println(tree.getInfo); - We print the blue () node. preordertraversal(tree.getleft) - Visit the red () node: We call the procedure again using the red () node as the tree. We print the root (red () ) and note that there is no left node of red () or no right node of red (), so we exit the recursive call. IS265/506: hapter 08 Intro to inary Trees 24

Very Simple ase (ontinued) blue red black preordertraversal(tree.getright) - Visit the black () node: We call the procedure again using the black () node as the tree. We print the root (black () ) and note that there is no left node of black () or no right node of black (), so we exit the recursive call. /* end if */ - We are done. Our output is: blue () red () black () IS265/506: hapter 08 Intro to inary Trees 25 Let s do one that is more difficult E D F E D F IS265/506: hapter 08 Intro to inary Trees 26

PostOrder Traversal Postorder visits the the left child, the right child, then the root The algorithm for POSTORDER Traversals: ssume the tree exists Visit the left subtree Visit the right subtree Visit the root IS265/506: hapter 08 Intro to inary Trees 27 Let s Look at the ode // Postorder visits // left child, right child, then the root void postordertraversal(nodeptr tree) { if (tree!= NULL) { postordertraversal( tree.getleft() ); postordertraversal( tree.getright() ); System.out.println( tree.getinfo() ); } } IS265/506: hapter 08 Intro to inary Trees 28

Very Simple ase blue red black Is the tree NULL? No, it does exist postordertraversal(tree.getleft) - Visit the red () node: We call the procedure again using the red () node as the tree. We note that there is no left node of red () or no right node of red, so we print the root (red () ) and exit the recursive call. IS265/506: hapter 08 Intro to inary Trees 29 Very Simple ase (ontinued) blue red black postordertraversal(tree.getright) - Visit the black () node: We call the procedure again using the black () node as the tree. We note that there is no left node of black () or no right node of black (), so we print the root (black () ) and exit the recursive call. System.out.println(tree.getInfo); - We print the blue() node. /* end if */ - We are done. Our output is: red () black () blue () IS265/506: hapter 08 Intro to inary Trees 30

Let s do one that is more difficult E D F D F E IS265/506: hapter 08 Intro to inary Trees 31