CSI33 Data Structures

Similar documents
CSI33 Data Structures

Binary Trees, Binary Search Trees

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

Chapter 20: Binary Trees

March 20/2003 Jayakanth Srinivasan,

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. (Trees) Data Structures and Programming Spring / 28

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

CE 221 Data Structures and Algorithms

Binary Trees

Trees. Trees. CSE 2011 Winter 2007

TREES. Trees - Introduction

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

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

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

Data Structure - Binary Tree 1 -

Introduction to Computers and Programming. Concept Question

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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search 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

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

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

Trees. Tree Structure Binary Tree Tree Traversals

Trees. Truong Tuan Anh CSE-HCMUT

Trees, Binary Trees, and Binary Search Trees

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

Stacks, Queues and Hierarchical Collections

Trees. CSE 373 Data Structures

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Why Do We Need Trees?

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

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


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

CS350: Data Structures Tree Traversal

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

An Introduction to Trees

Associate Professor Dr. Raed Ibraheem Hamed

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

Binary Trees. Examples:

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

Analysis of Algorithms

Programming II (CS300)

DATA STRUCTURES USING C

Data Structures and Algorithms for Engineers

Introduction to Binary Trees

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

12 Abstract Data Types

Binary Trees. Height 1

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

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Trees: examples (Family trees)

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Data Structures Question Bank Multiple Choice

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

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

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

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

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

Advanced Tree Data Structures

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

CSI33 Data Structures

CSI33 Data Structures

Algorithms and Data Structures

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

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

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

BBM 201 Data structures

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

IX. Binary Trees (Chapter 10)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Garbage Collection: recycling unused memory

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

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

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

Topic Binary Trees (Non-Linear Data Structures)

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

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

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

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

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

3137 Data Structures and Algorithms in C++

LECTURE 13 BINARY TREES

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

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Postfix (and prefix) notation

Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid

Trees. Eric McCreath

Chapter 5. Binary Trees

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

CS301 - Data Structures Glossary By

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

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

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017

Outline Outline 1 C++ Supplement.1: Trees

Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees

Uses Of Trees C++ Supplement.1: Trees Taxonomies animal vertebrate invertebrate fish mammal reptile insect

Uses Of Trees C++ Supplement.1: Trees Hierarchies Board Of Directors CEO COO Administrative Staff... Division Executives Managers...

Uses Of Trees C++ Supplement.1: Trees Efficient Containers of Sequential Data Each node represents a single data item of a collection organized, for efficient access, into a tree: Binary Search Trees Heaps Priority Queues B-Trees, Quad Trees, etc.

Definitions C++ Supplement.1: Trees Definitions I A tree consists of nodes connected by edges. A node can have zero or more child nodes. A child is connected to its parent node by a single edge. Children with the same parent are called siblings. A tree with no nodes or edges is an empty tree. A nonempty tree will have one special node with no parent called the root node. A node with no children is called a leaf.

Definitions C++ Supplement.1: Trees Definitions II All nodes are connected to the root by a path of edges. The depth of a node is the length of its path to the root. The root has depth zero. A level of a tree is a set of nodes which have the same depth. The descendants of a node are nodes whose paths include that node. The ancestors of a node are nodes on its path to the root.

Binary Trees C++ Supplement.1: Trees Binary Trees A tree whose nodes have at most two children is a binary tree. 2 7 6 5 8 4 3

Binary Trees C++ Supplement.1: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5

Binary Trees C++ Supplement.1: Trees Complete Binary Trees A complete binary tree has every level filled except the bottom, which is filled from left to right. 2 7 6 3 5 8 4 1 9 2

Binary Tree Traversal Binary Trees are Recursive Data Structures A binary tree can be defined as either An empty binary tree (base case!), or a node having two binary trees as attributes, a left subtree and a right subtree (recursive case). Using this recursive definition, recursive algorithms can be used to process the nodes of a binary tree.

Binary Tree Traversal Preorder def traverse(tree): if tree is not empty: process data at tree s root traverse(tree s left subtree) traverse(tree s right subtree)

Binary Tree Traversal Inorder def traverse(tree): if tree is not empty: traverse(tree s left subtree) process data at tree s root traverse(tree s right subtree)

Binary Tree Traversal Postorder def traverse(tree): if tree is not empty: traverse(tree s left subtree) traverse(tree s right subtree) process data at tree s root

Binary Tree Representation Of An Expression Abstract Syntax Tree + * * + 4 5 6 2 3

Binary Tree Representation Of An Expression Different Traversals Give The Different Notations + * * + 4 5 6 2 3 Printing in preorder gives Prefix Notation. Printing inorder gives Infix Notation. Printing in Postorder gives Postfix Notation.

Binary Tree Representation Of An Expression Evaluation + * * + 4 5 6 2 3 def evaluatetree(tree): if tree s root is an operand: return root data else: # root contains an operator leftvalue = evaluatetree(tree s left subtree) rightvalue = evaluatetree(tree s right subtree) result = operator(leftvalue, rightvalue) return result

Linked Representation Of A Binary Tree in C++ Use A TreeNode Class A binary tree has a single instance variable, root, a pointer to a TreeNode. A nonempty binary tree is represented with root as a pointer to an actual TreeNode object. An empty binary tree has no root. We represent this by using a value of NULL for the instance variable root. as a pointer to a TreeNode. The TreeNode class is defined recursively.

Linked Representation Of A Binary Tree in C++ The TreeNode class is defined recursively. The left and right instance variables are pointers to TreeNode objects, if there are left or right subtrees, or NULL if those subtrees are empty. The item instance variable is the data item stored in the TreeNode.

Linked Representation Of A Binary Tree in C++ (Simplified) Definition Of A TreeNode Class in C++ class TreeNode { public: TreeNode(int item, TreeNode* left = NULL, TreeNode* right = NULL); ~TreeNode(); // destructor int item; // value TreeNode* left; // link to left subtree TreeNode* right; // link to right subtree };

Linked Representation Of A Binary Tree in C++ C++ Examples Using The TreeNode Class left item right TreeNode None

Linked Representation Of A Binary Tree in C++ Example 1: Abstract View 2 1 3

Linked Representation Of A Binary Tree in C++ Example 1: C++ Implementation TreeNode * left = new TreeNode(1); TreeNode * right = new TreeNode(3); TreeNode * root = new TreeNode(2, left, right);

Linked Representation Of A Binary Tree in C++ Example 1: C++ Memory Model left item 2 right left item right left item right None 1 None None 3 None

Linked Representation Of A Binary Tree in C++ Example 2: Abstract View 2 7 6 5 8 4 3

Linked Representation Of A Binary Tree in C++ Example 2: C++ Implementation root = new TreeNode(2, new TreeNode(7, NULL, new TreeNode(5, new TreeNode(3))), new TreeNode(6, new TreeNode(8), new TreeNode(4)));

Linked Representation Of A Binary Tree in C++ Example 2: C++ Memory Model left item right 2 left item right left item right None 7 6 left item right left item right left item right 5 None None 8 None None 4 None left item right None 3 None

Array Representation of Binary Trees Abstract View 2 7 6 5 8 4 3

Array Representation of Binary Trees Implementation 2 7 6 NULL 5 8 4 NULL NULL 3 1 2 3 4 5 6 7 8 9 10 def left child(i): return 2 * i def right child(i): return 2 * i + 1 def parent(i): return i // 2