This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees.

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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
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.
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.
COSC2007 Data Structures II
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
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.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Week 6 - Wednesday CS221.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Design and Analysis of Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees.
Presentation transcript:

This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees

Tree a set of leaves connected by branches – you can get to any leaf by following some path from the root a set of nodes connected by edges the first node is the root interior nodes provide information and links

Tree Usually diagrammed upside down There is only one root node Information is stored in the nodes The edges represent some relationship between the nodes

Terminology parent children siblings descendents ancestors leaf node non-leaf node subtree path  direct ancestor  direct descendents  have same parent  children & children's children  parent & parent's parents  no children  has children  node and its descendents  nodes and edges to a node

Diagrams L treeR tree node L edgeR edge subtree

Familiar Uses Organizational Chart Family Tree Inheritance Hierarchy

Organization Chart Shows the personnel of a company – Each node represents a person or title – Each edge indicates supervisor / subordinate relationship

Widgets, Inc President / CEO VP SalesVP Service Human Resources Mgr Eastern Region Mgr Western Region Mgr Telephone Support Mgr Returns/Repairs Agents VP Research & Development Project Manger Agents Engineers Technicians Documentation Testing Operators Technicians height = 4

Family Tree Nodes are individuals Edges represent the parent / child relationship Siblings have the same parent

My Ancestors Me MotherFather Maternal Grandmother Maternal Grandfather Paternal Grandmother Paternal Grandfather Great Grandmother Great Grandfather Great Grandmother Great Grandfather Great Grandmother Great Grandfather Great Grandmother Great Grandfather

Descendents Great Grandparents DaughterSon Grand child Son-in-law Daughter-in-law Son Daughter-in-law Grand child great grandchild

Inheritance Hierarchy Object Throwable Component Comparable Exception Error Frame Window

A Tree of Trees Tree Examples Organization Chart Family Tree Inheritance Hierarchy

Not so Familiar Uses Algebraic Expressions Decision trees (flow chart) – Binary Search Trees Parse trees (based on grammar) Game trees (possible moves)

Which nodes are … leaf nodes? siblings of G? ancestors of L? descendents of D? children of I? A CD FIE B RSP GH T ML O KJ Q N

More Terms height length of path left child right child left subtree right subtree  number of levels  1 + height of tallest subtree  number of edges of the path  first child  second or last child  left child & its descendents  right child & its descendents

Tree Types General n-ary Binary – Binary Search Tree (bst) – Expression – Parse – Heap

General any number of children per node no max A CD FIE B RSP GH T ML O KJ Q N

n-ary no more than n children per node 3-ary A CD FIE B RSP GH T ML O KJ Q N

Binary Trees very common at most 2 children per node each subtree is also a binary tree Full - when every node in all levels, except the last level, has 2 children. Nodes in last level have 0 children Complete - full to next to last level & last level is filled from left to right Other - not full or complete

Full, Complete or Other? A CD FIE B RSP GH T ML O KJ Q N not binary

Full, Complete or Other? A D FI B RSP GH T ML O K N

A D F I B R S P G H T M L O K N

A D F I B R S P G H T M L O K N

A D F I B R S P G H T M L O K N

A D F I B Q S R G H T M L P K N O

A D F I B Q S R G H T M L P K N O

A D F I B QR G H M L P K N O

Traversals Visit each node only once (process data in the node) – Which order? – How do we keep track? Mark the node or Distinguish subtrees from the root and visit based on an algorithm

Preorder Traversals 1.Visit the root 2.Visit Left subtree 3.Visit Right subtree A D F I B R S P G H T M L O K N

Inorder Traversals 1.Visit Left subtree 2.Visit the root 3.Visit Right subtree A D F I B R S P G H T M L O K N

Postorder Traversals 1.Visit Left subtree 2.Visit Right subtree 3.Visit the root A D F I B R S P G H T M L O K N

Level Order Traversals 1.Visit the root 2.Visit root of Left subtree 3.Visit root of Right subtree 4.??? B H M N A D F I B R S P G H T M L O K N

Level Order Traversal w/Queue 1.Enqueue the root node 2.while queue is not empty a)Dequeue the front of queue to current b)Visit current node c)Enqueue all children of current

Binary Search Tree special type of binary tree node stores one Comparable data item left subtree (edge) is "less than" right subtree (edge) is "greater than"

Example of Binary Search Tree Full, Complete, or Other? Which traversal returns items in sorted order?

Full, Complete, or Other? Example of Binary SearchTree

Full, Complete, or Other? Example of Binary Search Tree

Full, Complete, or Other?