Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

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,
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
CSE 373 Data Structures Lecture 7
CS 201 Data Structures and Algorithms
Data Structures and Design in Java © Rick Mercer
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Data Structures and Design in Java © Rick Mercer
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
Objective: Understand Concepts related to trees.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
CHAPTER 4 Trees.
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 and Binary Trees.
Chapter 7. Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Trees CMSC 202, Version 5/02.
Binary Trees CS-2851 Dr. Mark L. Hornick.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
CE 221 Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Trees.
Tree and its terminologies
Binary Trees, Binary Search Trees
Heaps.
Trees.
Presentation transcript:

Trees

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 C B D E G F H J K I L M N

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

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> { V value; ArrayList children; } If we don’t care about the order of children, we might use a Set instead of a ArrayList

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

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

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

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

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

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

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

The End