CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Binary Tree B G E D I H F c A Binary tree
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann.
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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Topic 17 Introduction to Trees
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Comments? Linked lists –Last few comments about doubly linked lists Trees –Binary trees –Terminology –Binary tree example application –getting leftmost, rightmost nodes –preOrder, inOrder, postOrder traversals using recursion

Michael Eckmann - Skidmore College - CS Fall 2008 Tree terminology definitions A tree is a data structure consisting of a finite (possibly empty) set of nodes. If the tree is nonempty it has one root node. –All nodes in a tree can have 0 or more children. –All nodes in a tree have exactly one parent, except the root which has 0 parents. –Starting at any node in the tree you can trace a path back to the root by following the parent at each step. Picture on the board.

Michael Eckmann - Skidmore College - CS Fall 2008 Tree terminology definitions A binary tree is a data structure consisting of a finite (possibly empty) set of nodes. If the tree is nonempty it has one root node. –All nodes in a tree can have 0, 1 or 2 children. –All nodes in a tree have exactly one parent, except the root which has 0 parents. –Starting at any node in the tree you can trace a path back to the root by following the parent at each step. Picture on the board.

Michael Eckmann - Skidmore College - CS Fall 2008 Tree terminology definitions The root node in a tree is the one without a parent. The parent of a node n, in a tree, is the node that has n as one of its children. Leaf nodes are those that have 0 children. A subtree is part of a tree that has as its root any node in the original tree. The depth of a node is the number of steps away that node is from the root. The depth of the root is 0. The depth of the root's children are 1, etc. The depth of a tree is the maximum depth of any of its leaves.

Michael Eckmann - Skidmore College - CS Fall 2008 Tree terminology definitions (corrected)‏ A full binary tree is a binary tree where node has either 0 or 2 children. A perfect binary tree is a binary tree where every leaf has the same depth AND all internal nodes have degree 2. A complete binary tree is a binary tree where the leaves with the maximum depth are all on the left (and any other leaves are only one depth less). In other words, every level of the tree except the deepest level, must contain as many nodes as possible. At the deepest level the nodes must be as far left as possible.

Michael Eckmann - Skidmore College - CS Fall 2008 Tree terminology definitions Two nodes are siblings if they have the same parent. The ancestors of a node are all parents at each step up the tree to the root. The first ancestor of a node is it's parent. The second ancestor of a node is it's parent's parent. And so on until you reach the root which is an ancestor of every node in the tree. The descendents of a node are all it's children and their children's children etc.

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise How many nodes are there in a full binary tree of depth 8?

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise How many nodes are there in a full binary tree of depth 8? –root level has 1 node (which is 2 0 )‏ –next level has 2 nodes (which is 2 1 )‏ –next level has 4 nodes (which is 2 2 )‏ –next level has 8 nodes (which is 2 3 )‏ –next level has 16 nodes (which is 2 4 )‏ –next level has 32 nodes (which is 2 5 )‏ –next level has 64 nodes (which is 2 6 )‏ –next level has 128 nodes (which is 2 7 )‏ –next level has 256 nodes (which is 2 8 )‏ These added up are 2 9 – 1 = 512 – 1 = 511.

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise How many nodes are there in a complete binary tree of depth 4?

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise How many nodes are there in a complete binary tree of depth 4? –root level has 1 node (which is 2 0 )‏ –next level has 2 nodes (which is 2 1 )‏ –next level has 4 nodes (which is 2 2 )‏ –next level has 8 nodes (which is 2 3 )‏ –next level has anywhere from 1 to 16 nodes So, adding these is anywhere from 16 to 31 nodes. When a binary tree has 31 nodes is it guaranteed to be a full binary tree?

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise When a binary tree has 2 n – 1, where n is an integer >= 0, nodes and all nodes have either 0 or 2 children and all leaves are at the same depth is it guaranteed to be a full binary tree? Is it guaranteed to be a complete binary tree?

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise When a binary tree has 2 n – 1, where n is an integer >= 0, nodes and all nodes have either 0 or 2 children and all leaves are at the same depth is it guaranteed to be a full binary tree? –Yes Is it guaranteed to be a complete binary tree? –Yes, all full binary trees are complete Is a binary tree of 12 nodes –full? –complete?

Michael Eckmann - Skidmore College - CS Fall 2008 Exercise Is a binary tree of 12 nodes –full? No –complete? possibly

Michael Eckmann - Skidmore College - CS Fall 2008 Application of binary trees The game 20 questions –The game consists only of yes/no questions Each node can contain a question (or answer)‏ If it contains a question, –If the answer to the question is yes then the left child node contains the next question to ask (or the answer to give)‏ –If the answer to the question is no then the right child node contains the next question to ask (or the answer to give)‏

Michael Eckmann - Skidmore College - CS Fall 2008 Application of binary trees What could you say about the nodes that contain answers, not questions? Assuming a full binary tree of depth 20 to be used for the game, how many possible answers could your tree differentiate between?

Michael Eckmann - Skidmore College - CS Fall 2008 Application of binary trees What could you say about the nodes that contain answers, not questions? –They are leaves. Assuming a full binary tree of depth 20 to be used for the game, how many possible answers could your tree differentiate between? 2 20 = (over 1 million)‏ that's the number of leaves The number of questions in the tree are 2 20 – 1 = The number of nodes in the tree are 2 21 – 1 =

Michael Eckmann - Skidmore College - CS Fall 2008 A few operations on binary trees getLeftmost node –Starting at root, follow the left until you hit a node whose left is null. That node is the leftmost node. getRightmost node –Starting at root, follow the right until you hit a node whose right is null. That node is the rightmost node. According to these definitions, will the leftmost and rightmost nodes always be leaves?

Michael Eckmann - Skidmore College - CS Fall 2008 A few operations on binary trees The leftmost node can have a right child and the rightmost node can have a left child, so the leftmost and rightmost nodes in a binary tree aren't necessarily leaves. Later we'll talk about how to create recursive methods to remove these nodes.

Michael Eckmann - Skidmore College - CS Fall 2008 Traversals of binary trees There are three typical ways to traverse a binary tree – preOrder, postOrder and inOrder. preOrder –process root –process nodes in left subtree with a recursive call –process nodes in right subtree with a recursive call Example on the board of a preOrder traversal.

Michael Eckmann - Skidmore College - CS Fall 2008 Traversals of binary trees postOrder –process nodes in left subtree with a recursive call –process nodes in right subtree with a recursive call –process root Example on the board of a postOrder traversal.

Michael Eckmann - Skidmore College - CS Fall 2008 Traversals of binary trees inOrder –process nodes in left subtree with a recursive call –process root –process nodes in right subtree with a recursive call Example on the board of an inOrder traversal. Applet:

Michael Eckmann - Skidmore College - CS Fall 2008 Implementing the traversals of binary trees Let's assume the processing we're doing to each node is just printing the data in that node. So, for preOrder traversal we need to do the following –print the root's data –do a preorder traversal of the left subtree –do a preorder traversal of the right subtree Notice the recursion?

Michael Eckmann - Skidmore College - CS Fall 2008 Implementing the traversals of binary trees The only issue is when to stop the recursion. To do a preOrder traversal of the left subtree there has to be a left subtree. If there is no left subtree (that is, left == null) then don't traverse that anymore. Same for a preOrder traversal of the right subtree - there has to be a right subtree to traverse. If there is no right subtree (that is, right == null) then don't traverse that anymore.

Michael Eckmann - Skidmore College - CS Fall 2008 Implementing the traversals of binary trees So, for preOrder traversal we need to do the following –print the root's data –If (left != null)‏ do a preorder traversal of the left subtree –If (right != null)‏ do a preorder traversal of the right subtree