1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.

Slides:



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

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Introduction to Trees Chapter 6 Objectives
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
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.
Tree Implementations Chapter Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
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
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.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Topic 17 Introduction to Trees
Tree Data Structures.
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:
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, Binary Trees, and Binary Search Trees COMP171.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
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.
Tree Implementations Chapter Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Trees.
Trees Chapter 15.
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Week 6 - Wednesday CS221.
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Trees.
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Tree.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project 4 is due Friday Nov 3 by 5 pm.. 2.The next OWL assignment is up and due on November 8 at 11 pm 3.HWK2 grades are on OWL 4.No….I haven’t (but I will this weekend!)

2 CMPSCI 187 TreesTrees Introduction to Introduction to Programming with Data Structures trees binary trees traversals of trees data structures for trees Balancing unbalanced trees

3 CMPSCI 187 Trees l Trees can represent a hierarchy l Corporate structure

4 CMPSCI 187 Trees l Table of contents of a book:

5 CMPSCI 187 Trees l Unix or Macintosh file system l Web page hierarchy File Directory cs121cs187 8 Queens Interfaces In Java GUI Design

6 CMPSCI 187 Trees and Tree Terminology l A tree is a data structure that consists of a set of nodes and a set of edges (or branches) that connect nodes. l Trees are our first example of a nonlinear data structure. H Components do not form simple sequences, like lists. H Trees represent hierarchies.

7 CMPSCI 187 Tree: Definition l A tree is a collection of elements (nodes) l Each node may have 0 or more successors F (Unlike a list, which has 0 or 1 successor) l Each node has exactly one predecessor F Except the starting / top node, called the root. F Root has zero predecessors

8 CMPSCI 187 Terminology l A is the root node. l B is the parent of D and E. (or ancester) l C is the sibling of B l D and E are the children of B. (or descendents) l D, E, F, G, I are external nodes, or leaves l A, B, C, H are internal nodes l The depth (level) of E is 2 l The height of the tree is 3 l The degree of node B is 2 Property: (# edges) = (#nodes) - 1

9 CMPSCI 187 More tree terminology l Subtree of a node: A tree whose root is a child of that node l Level of a node: A measure of its distance from the root: Level of the root = 0 Level of other nodes = 1 + level of parent

10 CMPSCI 187 Binary Trees A Family Tree is a binary tree (each node has 0, 1, or 2 branches) Computer scientists like to look at tree upside down!

11 CMPSCI 187 Binary Trees l A binary tree is an ordered tree in which every node has at most two children (or at most two non- empty subtrees). l A set of nodes T is a binary tree if: H T is empty H or T consists of a s root node (internal node) s a left binary tree and s a right binary tree ((((3 x (1 + (4 + 6))) + (2 + 8)) x 5) + ( 4 x (7 + 2)))

12 CMPSCI 187 Expression Trees + * b / a 26 a/2 + 6*b

13 CMPSCI 187 Expression Trees: An Aside I Note that we can traverse this entire structure without any backpointers from child nodes to parent nodes. Whenever we need to back up, we just pop the stack and pick up where we left off. Recursion makes traversing binary trees easy. public void infix () { if (isEmpty ()) return; left.infix (); System.out.println (value ()); right.infix (); } + * b / a 26 a/2 + 6*b

14 CMPSCI 187 Expression Trees: An Aside II a 2 / 6 b * + hmmmm….looks like the postfix form of the expression public void postfix () { if (isEmpty ()) return; left.postfix (); right.postfix (); System.out.println (value ()); } + * b / a 26

15 CMPSCI 187 Binary Tree Example Decision Tree Rao’s

16 CMPSCI 187 Binary Sort Trees l Binary sort (or search) trees have this useful property: An inorder traversal of the tree will process the items in increasing order. F Relationship to binary search???? F Items in left subtree < item at root < items in right subtree

17 CMPSCI 187 Properties of Binary Trees l (# external nodes ) = (# internal nodes) + 1 (# nodes at level i)  2 i (# external nodes)  2 (height) (height)  log 2 (# external nodes) (height)  log 2 (# nodes) - 1 (height)  (# internal nodes) = ((# nodes) - 1)/2

18 CMPSCI 187 Full Binary Trees l In a full binary tree, H every leaf node has the same depth H every non-leaf node (internal node) has two children. A B DE H IJK C FG L Not a full binary tree. A B DE H IJK C FG L MNO A full binary tree.

19 CMPSCI 187 Complete Binary Tree l In a complete binary tree H 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). H 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

20 CMPSCI 187 A Representation for Complete Binary Trees l 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

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

22 CMPSCI 187 Proper Binary Tree l In a proper binary tree, F each node has exactly zero or two children F 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 LM

23 CMPSCI 187 Not Complete or Full or Proper A B D E H IJK C F L

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

25 CMPSCI 187 Binary Tree Nodes Data Left Right {Could put in a reference to a node's parent as well} Data Left Right Data Left Right

26 CMPSCI 187 Tree Implementation Plan l Different from Koffman and Wolfgang l Define interfaces for F BinaryTreeNode F Binary Tree l Do full implementation of a binary tree F Complete code F But we’ll only hit the highlights in lecture l Read the book to see what they did F Very restricted binary tree implementation F Use it to define binary sort trees F Never does a standard binary tree implementation l Jave does even less … we’ll see later

27 CMPSCI 187 An Interface for Nodes in a Binary Tree public interface BinaryNodeInterface { public Object getData(); //returns the data element at this node. public void setData(Object myElement); //sets data element to myElement public void setLeftChild(BinaryNodeInterface myLeft); //set left child to myLeft public void setRightChild(BinaryNodeInterface myRight); //set right child to myRight public BinaryNodeInterface getLeftChild();//returns left child node public BinaryNodeInterface getRightChild(); //returns right child node public boolean hasLeftChild(); //returns true if this node has a left child public boolean hasRightChild(); //returns true if this node has a right child public boolean isLeaf(); //returns true if node is a leaf node. } Basically the set and get methods for the data fields of the binary tree node……….

28 CMPSCI 187 The BTreeNode Class public class BinaryNode implements BinaryNodeInterface, java.io.serializable { protected Object data; protected BinaryNode left; protected BinaryNode right; //Constructors public BinaryNode() {this(null);} public BinaryNode(Object dataElement) { this(dataElement, null,null; }

29 CMPSCI 187 The BTreeNode Class Constructor public BinaryNode(Object dataElement, BinaryNode leftChild, BinaryNode rightChild) { data = dataElement; left = leftChild; right = rightChild; }

30 CMPSCI 187 The BTreeNode Class public boolean isLeaf() { return ((left == null) && (right == null)); } public Object getData() { return data;} public void setData(Object newData) { data=newData;}

31 CMPSCI 187 The BTreeNode Class public void setLeftChild(BinaryNodeInterface leftChild) { left = leftChild;} public BinaryNodeInterface getLeftChild() { return left;} public void setRightChild(BinaryNodeInterface rightChild) { right = rightChild;} public BinaryNodeInterface getRightChild() { return right;} Set,Get LeftChild Set,Get RightChild

32 CMPSCI 187 The BTreeNode Class Public boolean hasLeftChild() { return left != null } Public boolean hasRightChild() { return right != null } }//end Binary Node

33 CMPSCI 187 Operations on Binary Trees l Common operations on trees: F Create a tree F Determine if a tree is empty F Empty a tree F Set and Get the element at the root F Get the left and right subtrees at the root F Create a left and right subtree for the root F Attach a tree as the left or right subtree of the root F Detach the left or right subtree of the root l Can now define an ADT for the binary tree root: tree1: root: tree1:

34 CMPSCI 187 The BinaryTree Class