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

Slides:



Advertisements
Similar presentations
Trees Chapter 11.
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
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.
COSC2007 Data Structures II
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 
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
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.
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.
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Data Structures TREES.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
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.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
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.
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
Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees and Binary Trees.
General Trees & Binary Trees
Lecture 36 Section 12.2 Mon, Apr 23, 2007
General Trees & Binary Trees
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Tree and its terminologies
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

COSC2007 Data Structures II Chapter 10 Trees I

2 Topics Terminology

3 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

4 Introduction & Terminology Hierarchical (Tree) structure: Parent-child relationship between elements of the structure Nonlinear One-to-many relationships among the elements Examples: Organization chart Library card-catalog Contents of Books More???

5 Introduction & Terminology Corporate structure

6 Introduction & Terminology Example: Library card catalog Card Catalog Subject Catalog Author CatalogTitle Catalog A - AbbexStafford, R - Stanford, RZon - Zz Stafford, R. H.Standish, T. A. Stanford Research Institute

7 Introduction & Terminology Table of contents of a book:

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

9 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

10 Introduction & Terminology Path from node n 1 to n k : A sequence of nodes n 1, n 2, …. n k such that there is an edge between each pair of nodes (n 1, n 2 ) (n 2, n 3 ),... (n k-1, n k ) Height h: The number of nodes on the longest path from the root to a leaf Ancestor-descendent relationship Generalization of parent-child 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

11 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) the lies directly below it Each node has at most one predecessor (parent) the lies directly above it

12 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

13 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

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

15 Number Of Nodes & Height Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2 h – 1 log 2 (n+1) <= h <= n

16 A B DE H IJK C FG L MNO 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 DE H IJK C FG L Not a full binary tree.A full binary tree.

17 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 DE H IJK C FG L Not a Complete Binary Tree A B DE H IJK C FG L A Complete Binary Tree

18 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 DE H IJK C FG L A B DE H IJK C FG Not a proper binary treeA proper binary tree

19 An Aside: A Representation for Complete Binary Trees Since tree is full, it maps nicely onto an array representation. A B DE H IJK C FG L A B C D E F G H I J K L T: last

20 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 its 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.

21 Not Complete or Full or Proper A B D E H IJK C F L

22 Balanced vs. Unbalanced Trees 1 A B DE H IJK C FG L root A B D E H I JK C FG L start Sort of Balanced Mostly Unbalanced A binary tree in which the left and right subtrees of any node have heights that differ by at most 1

23 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

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