Tree Implementations Chapter 16. 2 Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary.

Slides:



Advertisements
Similar presentations
Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.
Advertisements

1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
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.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
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
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Course: Programming II - Abstract Data Types Slide Number 1 The ADT Binary Tree Definition The ADT Binary Tree is a finite set of nodes which is either.
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.
© 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.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
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,
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
1 Binary Trees (7.3) CSE 2011 Winter November 2015.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
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.
CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation.
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.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Tree Implementations Chapter Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
Trees Saurav Karmakar
Trees Chapter 11 (continued)
Basic Tree Data Structures
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Data Structures Binary Trees 1.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees What is a Tree? Tree terminology Why trees?
Tree Traversals – reminder
A Binary Search Tree Implementation
Stacks Linked Lists Queues Heaps Hashes
Tree Implementations (plus briefing about Iterators)
Tree Traversals – reminder
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Tree Traversal Methods
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Tree Traversal Methods
Trees.
Slides by Steve Armstrong LeTourneau University Longview, TX
Trees Definitions Implementation Traversals K-ary Trees
Binary Tree Traversal Methods
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
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.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Tree Implementations Chapter 16

2 Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary tree. We could use an array to implement a tree. However, it is attractive only when the tree is complete. In such cases, the link between a parent and child is not stored explicitly.

3 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(); public int getHeight(); public int getNumberNodes(); } // end BinaryNodeInterface

4 Implementation of BinaryNode Usually the class that represents a node in a tree is a detail hidden from the client, e.g, node is private to link list. Since any class that extends our binary tree class might need to manipulate nodes, we define tree nodes outside of our binary tree class. However, the tree node is not public. It is placed within a package TreePackage Available to all classes in the same package involved in the implementation of a tree

5 Implementation of BinaryNode package TreePackage Class BinaryNode implements BinaryNodeInterface { private T data; private BinaryNode left; private BinaryNode right; public BinaryNode() { …. } …. }

6 Computing Height, Counting Nodes Within BinaryTree getHeight() { return root.getHeight();// public getHeight method } getNumberOfNodes() { return root.getNumberOfNodes(); } Implementation within BinaryNode is easier private getHeight(BinaryNode node) //tree rooted at the invoking node

7 Recursive getHeight private int getHeight(BinaryNode node) { int height = 0; if(node != null ) height = 1 + Math.max(getHeight(node.left), getHeight(node.right)); return height; }

8 Question How to write a recursive method to compute the total number of nodes in a binary tree?

9 Recursive getNumberOfNodes public int getNumberOfNodes() { …. }

10 Class BinaryTree package TreePackage; public class BinaryTree implements BinaryTreeInterface { private BinaryNodeInterface root; public BinaryTree(T rootData) { root = new BinaryNode ( rootData) } public BinaryTree( T rootData, BinaryTree leftTree, BinaryTree rightTree) { root = new BinaryNode (rootData); if(leftTree !=null) root.setLeftChild(leftTree.root); if(rightTree != null) root.setRightChild(rightTree.root); …. } …. }

11 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

12 Traversals A binary tree.

13 Traversals Using a stack to perform an inorder traversal of the binary tree.. Start from pushing the root, then traverse to the left as far as possible, pushing each node, then pop, if has right children, push again.

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

15 General Trees A node for a general tree. Accommodate any number of children by referencing an object such as a list that contains the children. Two references, one reference is to the data object, and the other is to a list of child nodes.