Tree Implementations Chapter 25. 2 Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.

Slides:



Advertisements
Similar presentations
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Advertisements

Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
Trees. 2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
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.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Trees.
Planning & System installation
Trees Chapter 15.
Binary Trees.
Trees Chapter 11 (continued)
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Planning & System installation
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Planning & System installation
A Binary Search Tree Implementation
Binary Tree and General Tree
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Implementations (plus briefing about Iterators)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Tree Traversal Methods
General Trees & Binary Trees
Binary Tree Traversal Methods
Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees
Trees.
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 16 Tree Implementations
Binary Tree Traversal Methods
General Trees & Binary Trees
Binary Trees.
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Tree Traversal Methods
These notes are intended for use by students in CS0445 at the University of Pittsburgh and no one else These notes are provided free of charge and may.
A Binary Search Tree Implementation
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Tree Implementations Chapter 25

2 Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation of the ADT Binary Tree Creating a Basic Binary Tree The Method privateSetTree Accessor and Mutator Methods Computing the Height and Counting Nodes Traversals An Implementation of an Expression Tree General Trees A Node for a General Tree Using a Binary Tree for Represent a General Tree

3 Nodes in a Binary Tree Fig A node in a binary tree.

4 Interface for a Node Interface for class of nodes suitable for a binary tree public interface BinaryNodeInterface {public Object getData(); public void setData(Object newData); public BinaryNodeInterface getLeftChild(); public BinaryNodeInterface getRightChild(); public void setLeftChild(BinaryNodeInterface leftChild); public void setRightChild(BinaryNodeInterface rightChild); public boolean hasLeftChild(); public boolean hasRightChild(); public boolean isLeaf(); } // end BinaryNodeInterface

5 Implementation of BinaryNode Usually the class that represents a node in a tree is a detail hidden from the client It is placed within a package Makes it available Available to all classes involved in implementation of a tree

6 An Implementation of the ADT Binary Tree Recall interface for class of binary trees from previous chapterinterface In that interface TreeInterface specifies basic operations common to all trees TreeInteratorInterface specifies operations for tree traversal Note full implementation of BinaryTree, section 25.4 in text.

7 The Method privateSetTree Problem: previous implementation of privateSetTree not sufficient Given: treeA.setTree (a, treeB, treeC); Now treeA shares nodes with treeB, treeC Changes to treeB also affect treeA (Fig. 25-2) fig 25-2 here

8 The Method privateSetTree Possible solution Have privatSetTree copy the nodes in TreeB and TreeC Now treeA is separate and distinct from treeB and treeC Changes to either treeB or treeC do NOT affect treeA Note that copying nodes is expensive Other solutions are considered

9 The Method privateSetTree Another possible solution for treeA.setTree( a, treeB, treeC); Have privateSetTree first link the root node of treeA to root nodes of treeB, treeC (Fig. 25-3) Then set treeB.root, treeC.root to null fig 25-3 here This still has some problems

10 The Method privateSetTree Real solution should do the following: 1. Create root node r for containing the data 2. If left subtree exists, not empty Attach root node to r as left child 3. If right subtree exists, not empty, distinct from left subtree Attach root node r as right child If right, left subtrees are the same, attach copy of right subtree to r instead 4. If left (right) subtree exists, different from the invoking tree object Set its data field root to null

11 Accessor, Mutator Methods Methods implemented getRootData() isEmpty() clear() setRootData (Object rootData) setRootNode (BinaryNode rootNode) getRootNode()

12 Computing Height, Counting Nodes Within BinaryTree getHeight() getNumberOfNodes() Within BinaryNode getHeight() getHeight(BinaryNode node) getNumberOfNodes()

13 Traversals Make recursive method private Call from public method with no parameters public void inorderTraverse() {inorderTraverse(root);} private void inorderTraverse(BinaryNode node) {if (node != null) {inorderTraverse((BinaryNode)node.getLeftChild()); System.out.println(node.getData()); inorderTraverse((BinaryNode)node.getRightChild()); } // end if } // end inorderTraverse

14 Traversals Fig A binary tree.

15 Traversals Fig Using a stack to perform an inorder traversal of the binary tree in Fig

16 Traversals Fig Using a stack to traverse the binary tree in Fig in (a) preorder

17 Traversals Fig Using a stack to traverse the binary tree in Fig in (b) postorder.

18 Traversals Fig Using a queue to traverse the binary tree in Fig in level order.

19 Implementation of an Expression Tree Defining an interface for an expression tree Extend the interface for a binary tree Add a declaration for the method evaluate public interface ExpressionTreeInterface extends BinaryTreeInterface {/** Task: Computes the value of the expression in the tree. the value of the expression */ public double evaluate(); } // end ExpressionTreeInterface

20 General Trees Fig A node for a general tree.

21 Using a Binary Tree to Represent a General Tree Fig (a) A general tree;

22 Using a Binary Tree to Represent a General Tree Fig (b) an equivalent binary tree;

23 Using a Binary Tree to Represent a General Tree Fig (c) a more conventional view of the same binary tree.

24 Traversals Traversals of general tree in 25-9a Preorder: A B E F C G H I D J Postorder: E F B G H I C J D A Level order: A B C D E F G H I J Traversals of binary tree in 25-9c Preorder: A B E F C G H I D J Postorder: F E I H G J D C B A Level order: A B E C F G D H J I Inorder: E F B G H I C J D A