CS-240 Dick Steflik D.J. Foreman

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Trees CS-212 Dick Steflik. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
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 
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
Discrete Mathematics Chapter 5 Trees.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees (Unit 7).
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Non Linear Data Structure
Trees Chapter 15.
Binary Tree ADT: Properties
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Trees ---- Soujanya.
MCS680: Foundations Of Computer Science
Tree.
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.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
CHAPTER 4 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Trees & Forests D. J. Foreman.
Trees Definitions Implementation Traversals K-ary Trees
Principles of Computing – UFCFA3-30-1
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Trees Chapter 10.
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-240 Dick Steflik D.J. Foreman Trees CS-240 Dick Steflik 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 the root The remaining nodes are partitioned into n>0 disjoint sets T1,..,Tn, where each of these sets is a tree. T1,..,Tn are the subtrees of the root.

Examples - All are valid trees

Tree Terms Root height (h) interior nodes B and C are children of A A is the parent of B and C B and C are children of A height (h) B C B and C are siblings interior nodes D E F G H leaf nodes (exterior nodes)

height - number of nodes in the longest path from root to farthest leaf parent - any connected node in the tree at the next higher level in the tree child - any connected node in the tree at the next lower level in the tree siblings - any nodes in the tree having a common parent order - the number of children in the node having the largest number of children binary tree - any order 2 tree binary search tree - any binary tree having the search tree property (more on this later)

Nodal Structure Options If we know the maximum order that an arbitrary tree is supposed to be, we could allocate our data content and a child pointer for each possible child Ex suppose max. order = 5 each node would look like: child 1 child 2 child 3 child 4 child 5 Data If our tree has many nodes that have less than 5 children this representation could be very wasteful considering that each child pointer requires 4 bytes of storage. Is there a better, less wasteful representation?

As it turns out, YES there is The lowly order 2 (binary) tree can be used to represent any order n tree. and we can make the statement that: for any general tree, there is an equivalent binary tree. To do this we must visualize an order 2 tree differently; instead of a collection of parents and children we view it as parent, leftmost child (ONLY) and that child’s siblings . Instead of this : This: A A B B C D C D

Why do we want to do this? To explore this let us look at the problem of creating an algorithm for visiting every node in a tree in some predictable order. This problem is called Traversal and can be accomplished with the following the following algorithm. 1. start at the root and 2. follow all of the left links until you can’t go any farther 3. back-up one node and try going right, if you can, repeat steps 2 and 3, if you can’t, repeat step 3

Traversal Each node is visited 3 times Were we to print out the node data in the first visit to each node the printout would be : ABC Were we to printout the node data on the second visit to each node the printout would be: BAC A 1 3 2 Were we to printout the node data on the third visit to each node the printout would be: BCA 1 3 3 B C 1 These are called the: preorder, inorder and postorder traversals respectively 2 2

Pre-order Traversal template <typename T> void preorder( tnode<T> *t) { if (t != NULL) cout << t -> data << “ “; preorder(t -> left); preorder(t -> right); }

In-order Traversal template <typename T> void inorder( tnode<T> *t) { if (t != NULL) inorder(t -> left); cout << t -> data << “ “; inorder(t -> right; }

Post-order Traversal template <typename T> void postorder( tnode<T> *t) { if (t != NULL) postorder(t -> left); postorder(t -> right); cout << t -> data << “ “; }

Notice that... the first node visited on the pre-order traversal ia always the root the left-most node in the tree is the first node on the inorder traversal the last node visited on the inorder traversal is the rightmost node in the tree the last node visited on the postorder traversal is the root Knowing this and given any two traversal paths the tree can be constructed…….

Armed with this information… We should be able to construct the tree that produced any pair of traversal paths