Lab 4 Due date: March 29. Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element,
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
Binary Trees.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees.
Printing a search tree in order
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Data Structures Binary Trees 1.
Trees Another Abstract Data Type (ADT)
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Chapter 20: Binary Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 21: Binary Trees.
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
CS-240 Dick Steflik D.J. Foreman
Announcements: Assignment #2 is posted, due Wed. Oct. 7 (no extension). Lab 4 is posted. No office hours Friday Oct. 2. Let me know if you have any suggestions.
Binary Trees, Binary Search Trees
Binary Trees.
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Binary Tree Traversal.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Lab 4 Due date: March 29

Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required by an n node binary tree is n * (space required by one node).

The Struct binaryTreeNode template struct binaryTreeNode { T element; binaryTreeNode *leftChild, *rightChild; binaryTreeNode() {leftChild = rightChild = NULL;} // other constructors come here };

Linked Representation Example a cb d f e g h leftChild element rightChild root

// create a binary tree with root x binaryTreeNode *x, *y, *z; y = new binaryTreeNode (2); z = new binaryTreeNode (3); x = new binaryTreeNode (1, y, z);

visit void visit(binaryTreeNode *x) {// visit node *x, just output element field. cout element << ' '; treeSize++; }

Binary Tree Traversal Methods Preorder Inorder Postorder Level order

Preorder Example (visit = print) a bc abc

Preorder Traversal template void preOrder(binaryTreeNode *t) { if (t != NULL) { visit(t); preOrder(t->leftChild); preOrder(t->rightChild); }

Inorder Example (visit = print) a bc bac

Inorder Traversal template void inOrder(binaryTreeNode *t) { if (t != NULL) { inOrder(t->leftChild); visit(t); inOrder(t->rightChild); }

Postorder Example (visit = print) a bc bca

Postorder Traversal template void postOrder(binaryTreeNode *t) { if (t != NULL) { postOrder(t->leftChild); postOrder(t->rightChild); visit(t); }

Level Order Let t be the tree root. while (t != NULL) { visit t and put its children on a FIFO queue; if FIFO queue is empty, set t = NULL; otherwise, pop a node from the FIFO queue and call it t; }

queue *> q; while (t != NULL) { visit(t); // visit t // put t's children on queue if (t->leftChild != NULL) q.push(t->leftChild); if (t->rightChild != NULL) q.push(t->rightChild); // get next node to visit t = q.front(); if(!q.empty()) q.pop(); }

Level-Order Example (visit = print) a bc d e f g hi j abcdefghij

Practice create a tree as below, and test your code.

Implement functions Preorder traversal In-order traversal Post-order traversal Level-order traversal And test your program using previous example