COSC2007 Data Structures II

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
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
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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 A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Data Structures TREES.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
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 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 From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Trees and Binary Trees.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

COSC2007 Data Structures II April 21, 2017 COSC2007 Data Structures II Chapter 11 Trees I Trees

Topics Terminology

Introduction & Terminology Review Position-oriented ADT: Insert, delete, or ask questions about data items at specific position Examples: Stacks, Queues Value-oriented ADT: Insert, delete, or ask questions about data items containing a specific value Example?

Introduction & Terminology Hierarchical (Tree) structure: Parent-child relationship between elements of the structure More? Examples: Organization chart Library card-catalog More???

Introduction & Terminology Example: Library card catalog Card Catalog Subject Catalog Author Catalog Title Catalog A - Abbex Stafford , R - Stanford, R Zon - Zz Stafford, R. H. Standish, T. A. Stanford Research Institute

Introduction & Terminology Table of contents of a book:

Trees and Tree Terminology A tree is a data structure that consists of a set of nodes (vertex) and a set of edges (or branches) that connect nodes.

Introduction & Terminology A is the root node. B is the parent of D and E. C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The depth (level) of E is 2 The height of the tree is (3)/4 The degree of node B is 2 Property?

Introduction & Terminology Path from node n1 to nk: A sequence of nodes n1, n2, … . nk such that there is an edge between each pair of nodes (n1, n2) (n2, n3), . . . (nk-1, nk) Height h: The number of nodes on the longest path from the root to a leaf Ancestor-descendent relationship Ancestor of node n: A node on the path from the root to n Descendent of node n: A node on the path from n to a leaf

Introduction & Terminology Nodes: Contains information of an element Each node may contain one or more pointers to other nodes A node can have more than one immediate successor (child) that lies directly below it Each node has at most one predecessor (parent) that lies directly above it

Binary Trees A binary tree is an ordered tree in which every node has at most two children. A binary tree is: either empty or consists of a root node (internal node) a left binary tree and a right binary tree Each node has at most two children Left child Right child

Binary Trees Special trees Left (right) subtree of node n: In a binary tree, the left (right) child (if any) of node n plus its descendants Empty BT: A binary tree with no nodes Binary tree is position-oriented or value-oriented? But?

Number Of Nodes-height is h Suppose the height is h Minimum number of nodes in a binary tree: At least one node at each of first h levels. Maximum number of nodes in a binary tree: All possible nodes at first h levels are present

Number Of Nodes & Height Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2h – 1 If we know n, what will be the value for h??

Full Binary Trees In a full binary tree, every leaf node has the same depth every non-leaf node (internal node) has two children. A B D E H I J K C F G L A B C D E F G H I J K M N O L Not a full binary tree. A full binary tree.

Complete Binary Tree In a complete binary tree every level except the deepest must contain as many nodes as possible ( that is, the tree that remains after the deepest level is removed must be full). at the deepest level, all nodes are as far to the left as possible. A B D E H I J K C F G L A B D E H I J K C F G L Not a Complete Binary Tree A Complete Binary Tree

Proper Binary Tree In a proper binary tree, each node has exactly zero or two children each internal node has exactly two children. A B D E H I J K C F G L A B C D E F G H I J K Not a proper binary tree A proper binary tree

An Aside: A Representation for Complete Binary Trees Since tree is complete, it maps nicely onto an array representation. A B D E H I J K C F G L last T: A B C D E F G H I J K L 0 1 2 3 4 5 6 7 8 9 10 11 12

Properties of the Array Representation Data from the root node is always in T[0]. Suppose some node appears in T[i] data for its parent is always at location T[(i-1)/2] (using integer division) data for it’s children nodes appears in locations T[2*i+1] for the left child T[2*i+2] for the right child formulas provide an implicit representation of the edges can use these formulas to implement efficient algorithms for traversing the tree and moving around in various ways.

What Tree? A B D E H I J K C F L

Balanced vs. Unbalanced Binary Trees A binary tree in which the left and right subtrees of any node have heights that differ by at most 1 start A B C F G A B D E H I J K C F G L root L D H I E Mostly Unbalanced Sort of Balanced J K

Binary search tree (BST) 60 70 20 10 40 50 30 Binary search tree (BST) A binary tree where The value in any node n is greater than the value in every node in n’s left subtree The value in any node n is less than the value of every node in n's right subtree The subtrees are binary search trees too

Comparing Trees These two trees are not the same tree! A B A B Why?

Review Which of the following ADT is value-oriented? list sorted list stack queue binary tree

Review The ______ is a position-oriented ADT that is not linear. sorted list queue binary tree list

Review A node of a tree is called a(n) ______. edge root branch vertex

Review The lines between the nodes of a tree are called ______. branches edges Arches subtrees

Review The node that is directly above node n in a tree is called the ______ of node n. root leaf parent child

Review In a tree, the children of the same parent are called ______. leafs siblings roots contemporaries

Review Each node in a tree has ______. exactly one parent at most one parent exactly two parents at most two parents

Review A descendant of node n is a node on a path from node n to ______. the root a leaf a sibling of node n a child of node n

Review A subtree of node n is a subtree rooted at ______. node n the parent of node n a child of node n a sibling of node n

Review The ______ of a tree is the number of nodes on the longest path from the root to a leaf. height length width age

Review In a ______ of height h, all nodes that are at a level less than h have two children each. general tree binary tree full binary tree complete binary tree

Review A ______ of height h is full down to level h – 1, with level h filled in from left to right. full binary tree complete binary tree balanced binary tree general tree

Review In ______, the left and right subtrees of any node have heights that differ by at most 1. all trees all binary tress n-ary trees balanced binary trees

Review Which of the following is NOT a property of a complete binary tree of height h? all nodes at level h – 2 and above have two children each when a node at level h – 1 has children, all nodes to its left at the same level have two children each when a node at level h – 1 has one child, it is a left child all leaves are at level h

Review In an array based representation of a complete binary tree, which of the following represents the left child of node tree[i]? tree[i+2] tree[i–2] tree[2*i+1] tree[2*i+2]