Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

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

Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Tree Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Binary Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
CS 302 Data Structures Trees.
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Binary Trees.
Abstract Data Structures
Trees.
Binary Trees.
Binary Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees Chapter 10.
Binary Trees.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Trees Sciences OrganicMolecular MathPhysicsChemistryBiology QuantumAlgebra What is a tree? A data structure to represent hierarchical or sorted data

Trees: terminology A G D CB H F E I Node: any item of the tree Root Node: initial item of tree Parent: the parent of F is C Child: E is a child of B Leaf Node: A node with no children. Subtree A tree in its own right Ancestors (parent, parent of parent, etc.) Descendents (children, children of children, etc) Siblings (children with same parent)

Trees: storing with a struct A G D CB H F E I T(1).Data = {’A’ [12 5]}; T(2).Data = {’B’ [01 4]}; T(3).Data = {’C’ [-7 3]}; T(4).Data = {’D’ [41 0]}; T(5).Data = {’E’ [.7 9]}; T(6).Data = {’F’ [21 2]}; T(7).Data = {’G’ [14 1]}; T(8).Data = {’H’ [06 6]}; T(9).Data = {’I’ [97 4]}; T(1).Children = [2 3]; T(2).Children = [4 5]; T(3).Children = [6 7 8]; T(4).Children = []; T(5).Children = []; T(6).Children = []; T(7).Children = 9; T(8).Children = []; T(9).Children = []; In this example, the data at each node is a cell array, containing the node name and a 1-by-2 array of numerical information

Binary Tree If every node has 0, 1 or 2 children, then the tree is called a binary tree. The children of a node in a binary tree are referred to as Left and Right. A D C B G F E I L M J H Binary Search Trees: Used to store data that can be “ sorted ”, ie., a notion of ≤ exists for the data. The data at each node must have the property that D i ≤D k or D k ≤D i. Moreover, if D 1 ≤D 2 and D 2 ≤D 3 then it must be that D 1 ≤D 3. This gives efficient search routines. More in Weeks 11, 12 and 13.

Traversing a Tree An elementary and common operation on a tree is to start at the root node, and –traverse the tree (ie., visit every node), while… –performing some operation at each node (using, for example, the Data at the node) Recursive functions serve this purpose well. Two traversals: PreOrder and PostOrder InOrder for Binary trees A GD CB HFE I

PreOrder traversal of a Tree function preordop(Tree,Node) % First, do operation using Data % at the given Node of the Tree Operation(Tree(Node).Data); % Then loop through all Children, % calling preordop recursively for i=Tree(Node).Children preordop(Tree,i); end Note: As written here, based on Matlab syntax, the for loop requires that the children nodes be listed in a row vector. In this example, Operation represents any desired function to act on the data

Preorder traversal of a Tree function poprint(T,Node) disp(T(Node).Name) poprint(T,T(Node).Kids(1)) … poprint(T,T(Node).Kids(end)) >> poprint(Tree,1) At A, print A, do same A’s kids At B, print B, do same for B’s kids At D, print D. At E, print E. At C, print C, do same for C’s kids At F, print F At G, print G, do same for G’s kids At I, print I. At H, print H A GD CB HFE I ABEDCFIGH

Post-order traversal of a Tree function postordop(Tree,Node) % First loop through all Children, % calling postordop recursively for i=Tree(Node).Children postordop(Tree,i); end % Then do operation using the Data % at the given Node of the Tree Operation(Tree(Node).Data);

In-order traversal of a Binary Tree function inordop(BTree,Node) % First call inordop recursively % on the “left” child inordop(Tree,Tree(Node).Left); % Do operation using the Data % at the given Node of the Tree Operation(Tree(Node).Data); % Finally call inordop recursively % on the “right” child inordop(Tree,Tree(Node)).Right);

InOrder A D C B G F E function inordop(BTree,Node) % First call inordop % on the “left” child inordop(Tree,Tree(Node).Left); % Do operation using the Data % at the given Node of the Tree Operation(Tree(Node).Data); % Finally call inordop recursively % on the “right” child inordop(Tree,Tree(Node)).Right); D, B, E, A, F, C, G