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.

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.
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.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
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.
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.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Tree Data Structures.
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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
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.
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.
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.
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.
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 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.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
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 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
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
CS-240 Dick Steflik D.J. Foreman
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
Presentation transcript:

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 the root –The remaining nodes are partitioned into n>0 disjoint sets T 1,..,T n, where each of these sets is a tree. T 1,..,T n are the subtrees of the root.

Examples - All are valid trees

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

Definitions height - number of arcs 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: Data child 1 child 2 child 3 child 4 child 5 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. A BCD Instead of this : A B C D This :

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 A B C 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 Were we to printout the node data on the third visit to each node the printout would be: BCA These are called the: preorder, inorder and postorder traversals respectively

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

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

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

Notice that... the first node visited on the pre-order traversal is 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 can construct the tree that produced any pair of traversal paths