Trees. 2 Definition of a tree A tree is a node with a value and zero or more children Depending on the needs of the program, the children may or may not.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program,
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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,
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
data ordered along paths from root to leaf
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.
Starting at Binary Trees
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Lecture11: Tree I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures TREES.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
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.
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.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Problems with Linked List (as we’ve seen so far…)
Binary Trees our leafy, annoying friends
Trees.
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
Trees.
Trees.
Trees and Binary Trees.
Chapter 7. Trees & Binary Trees
Trees Definitions Implementation Traversals K-ary Trees
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Binary Trees, Binary Search Trees
Tree.
Trees.
Binary Trees, Binary Search Trees
Trees.
Presentation transcript:

Trees

2 Definition of a tree A tree is a node with a value and zero or more children Depending on the needs of the program, the children may or may not be ordered A tree has a root, internal nodes, and leaves Each node contains an element and has branches leading to other nodes (its children) Each node (other than the root) has a parent Each node has a depth (distance from the root) A CBDE GFHJKI LMN

3 More definitions An empty tree has no nodes The descendents of a node are its children and the descendents of its children The ancestors of a node are its parent (if any) and the ancestors of its parent The subtree rooted at a node consists of the given node and all its descendents An ordered tree is one in which the order of the children is important; an unordered tree is one in which the children of a node can be thought of as a set The branching factor of a node is the number of children it has The branching factor of a tree is the average branching factor of its nodes

4 Data structure for a tree Each node in a tree has an arbitrary number of children, so we need something that will hold an arbitrary number of nodes, such as an ArrayList class Tree { V value; ArrayList children; } If we don’t care about the order of children, we might use a Set instead of a ArrayList

5 ADT for a tree It must be possible to: Construct a new tree If a tree can be empty, this may require a header node Add a child to a node Get (iterate through) the children of a node Access (get and set) the value in a node It should probably be possible to: Remove a child (and the subtree rooted at that child) Get the parent of a node

6 File systems File systems are almost always implemented as a tree structure The nodes in the tree are of (at least) two types: folders (or directories), and plain files A folder typically has children—subfolders and plain files A folder also contains a link to its parent—in both Windows and UNIX, this link is denoted by.. In UNIX, the root of the tree is denoted by / A plain file is typically a leaf

7 Family trees It turns out that a tree is not a good way to represent a family tree Every child has two parents, a mother and a father Parents frequently remarry An “upside down” binary tree almost works Since it is a biological fact (so far) that every child has exactly two parents, we can use left child = father and right child = mother The terminology gets a bit confusing If you could go back far enough, it becomes a mathematical certainty that the mother and father have some ancestors in common

8 Part of a genealogy Isaac David Paul a Steven Danielle Winfred Carol Chester Elaine Eugene Pauline

9 Game trees Trees are used heavily in implementing games, particularly board games A node represents a position on the board The children of a node represent all the possible moves from that position More precisely, the branches from a node represent the possible moves; the children represent the new positions Planning ahead (in a game) means choosing a path through the tree However— You can’t have a cycle in a tree If you can return to a previous position in a game, you have a cycle Graphs can have cycles

10 Trees for expressions and statements Examples: The expression x > y ? x : y ?: >x x y y The statement if (x > y) max = x; else max = y; y if > xxy max ==

11 More trees for statements while (n >= 1) { exp = x * exp; n--; } for (int i = 0; i < n; i++) a[i] = 0; while >= n1 exp * = n -- x ; for i int = 0 a [ ]i i n0 ++=< i

12 The End