IMPLEMENTING BINARY TREES

Similar documents
Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Binary Trees Fall 2018 Margaret Reid-Miller

Computer Science II Fall 2009

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Course Project Hospital Information Management

Trees. Based on Chapter 8, Koffmann and Wolfgang. Chapter 8: Trees 1

Lecture 23: Binary Search Trees

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

1 public class BinaryTree<E extends Comparable<E>> { 2 private Node<E> root; 3 4 public BinaryTree (){ 5 root = null; 6 } 7 8 private BinaryTree

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Binary Tree Implementation

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

CS350: Data Structures Tree Traversal

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Module 6: Binary Trees

Binary Tree Implementation

CS 151. Binary Search Trees. Wednesday, October 10, 12

CSC 421: Algorithm Design & Analysis. Spring 2015

CSC 321: Data Structures. Fall 2016

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Examples

: Advanced Programming Final Exam Summer 2008 June 27, 2008

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

To implement binary trees, we have two prototypes: BTNode and BinaryTree.

void insert( Type const & ) void push_front( Type const & )

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Lecture 16. Lecture

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 151. Binary Trees. Friday, October 5, 12

Principles of Computer Science

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

Arun chakravarthy Alagar samy. Technical Skill. Java Code Challenge - Basic Java

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

CSC 321: Data Structures. Fall 2012

Programming II (CS300)

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

CSE 214 Computer Science II Heaps and Priority Queues

Facebook. / \ / \ / \ Accenture Nintendo

Tree Structures. A hierarchical data structure whose point of entry is the root node

Facebook. / \ / \ / \ Accenture Nintendo

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

If you took your exam home last time, I will still regrade it if you want.

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization

LECTURE 13 BINARY TREES

Title Description Participants Textbook

Binary trees. Binary trees. Binary trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Tutorial 4: Binary Trees

CMP-326 Final Exam Fall 2013 Total 120 Points Version 1

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Expression Trees and the Heap

Trees. 1 Introduction. 2 Trees. Prof. Stewart Weiss. CSci 235 Software Design and Analysis II Trees

CS II: Data Structures Discussion worksheet: Week 15

Tree Data Structures CSC 221

tree nonlinear Examples

Spring 2018 Mentoring 8: March 14, Binary Trees

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Topic 14. The BinaryTree ADT

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Introduction to Algorithms and Data Structures

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

About this exam review

Priority Queues. 04/10/03 Lecture 22 1

Computer Science 136 Exam 2

2018/2/5 话费券企业客户接入文档 语雀

Building Java Programs

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Title Description Participants Textbook

CSI33 Data Structures

ITI Introduction to Computing II

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

/department of mathematics and computer science 1/58

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Final Exam May 21, 2003

interface Position<E> { E getelement() throws IllegalStateExcept..; }

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

CMSC351 - Fall 2014, Homework #2

ITI Introduction to Computing II

CSI33 Data Structures

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

Tema 5. Ejemplo de Lista Genérica

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

Topic 7: Algebraic Data Types

Lecture 27. Binary Search Trees. Binary Search Trees

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Transcription:

IMPLEMENTING BINARY TREES Chapter 6 A Binary Tree BinaryTree<String> a = new BinaryTree<String>(); a A B C D E F 1

A Binary Tree BinaryTree<String> a = new BinaryTree<String>(); a A B C D E F A Binary Tree BinaryTree<String> a = new BinaryTree<String>(); a root A B C D E F 2

A Binary Tree BinaryTree<String> a = new BinaryTree<String>(); An Empty Tree a root = null a root A B C D E F Implementing a BinaryTree Class Section 6.3 3

- void preordertraverse(node<e> node, int depth, StringBuilder sb) { - void preordertraverse(node<e> node, int depth, StringBuilder sb) { 4

Node<E> Class Just as for a linked list, a node consists of a data part and links to successor nodes The data part is a reference to type E A binary tree node must have links to both its left and right subtrees Node<E> Class (cont.) public class Node<E> { public E data; public Node<E> left; public Node<E> right; public Node(E data) { this.data = data; left = null; right = null; } // Node() public String tostring() { return data.tostring(); } // tostring() } class Node<E> Node<E> is declared as an inner class within BinaryTree<E> 5

- void preordertraverse(node<e> node, int depth, StringBuilder sb) { - void preordertraverse(node<e> node, int depth, StringBuilder sb) { 6

* + / x y a b bt Assuming the tree is referenced by variable bt (type BinaryTree) then... bt * + / x y a b 7

bt bt.root.data references the Character object storing '*' bt * + / x y a b bt bt.root.left references the left subtree of the root bt * + / x y a b 8

bt.root.right references the right subtree of the root bt bt * + / x y a b bt bt.root.right.data references the Character object storing '/' 9

BinartTree<E> Class // Constructors public BinaryTree() { root = null; } // BinaryTree() public BinaryTree(Node<E> root) { this.root = root; } // BinaryTree() BinartTree<E> Class // Constructors public BinaryTree(E data, BinaryTree<E> lefttree, BinaryTree<E> righttree) { root = new Node<E>(data); if (lefttree!= null) { root.left = lefttree.root; } else { root.left = null; } if (righttree!= null) { root.right = righttree.root; } else { root.right = null; } } // BinaryTree() 10

Using BinaryTree<E> Class public static void main(string[] args) { // Create some Binary trees to put together // a tree [a [b [d][e]], [c [][f]]] BinaryTree<String> d = new BinaryTree<String>( D, null, null); BinaryTree<String> e = new BinaryTree<String>( E, null, null); BinaryTree<String> f = new BinaryTree<String>( F, null, null); BinaryTree<String> b = new BinaryTree<String>( B, d, e); BinaryTree<String> c = new BinaryTree<String>( C, null, f); BinaryTree<String> a = new BinaryTree<String>( A, b, c); System.out.println(a); } // main() d b a A B C D E F e c f - void preordertraverse(node<e> node, int depth, StringBuilder sb) { 11

- void preordertraverse(node<e> node, int depth, StringBuilder sb) { BinartTree<E> Class // Constructors public boolean isempty() { return this.root==null; } // isempty() public void clear() { this.root = null; } // clear() 12

- void preordertraverse(node<e> node, int depth, StringBuilder sb) { public class BinaryTreeException extends Exception { BinaryTreeException(String message) { super(message); } // BinaryTreeException() } // class BinaryTreeException public E getdata() { if (this.root == null) throw new BinaryTreeException( Accessing data in null tree ); return this.root.data; } // getdata() 13

- void preordertraverse(node<e> node, int depth, StringBuilder sb) { BinaryTree<E> Class public BinaryTree<E> getleftsubtree() { if (root!= null && root.left!= null) return new BinaryTree<E>(root.left); else return new BinaryTree<E>(); } // getleftsubtree() public boolean isleaf() { return (root == null) (root.left==null && root.right==null); } // isleaf() 14

- void preordertraverse(node<e> node, int depth, StringBuilder sb) Using BinaryTree<E> Class public static void main(string[] args) { // Create some Binary trees to put together // a tree [a [b [d][e]], [c [][f]]] BinaryTree<String> d = new BinaryTree<String>( D, null, null); BinaryTree<String> e = new BinaryTree<String>( E, null, null); BinaryTree<String> f = new BinaryTree<String>( F, null, null); BinaryTree<String> b = new BinaryTree<String>( B, d, e); BinaryTree<String> c = new BinaryTree<String>( C, null, f); BinaryTree<String> a = new BinaryTree<String>( A, b, c); System.out.println(a); } // main() d b a A B C D E F e c f 15

What to print? A B D null null E null null C null a A B C D E F F null null BinaryTree<E> Class public String tostring() { StringBuilder sb = new StringBuilder(); preordertraverse(this.root, 1, sb); return sb; } // tostring() 16

BinaryTree<E> Class public String tostring() { StringBuilder sb = new StringBuilder(); preordertraverse(this.root, 1, sb); return sb; } // tostring() private void preordertraverse(node<e> node, int depth, StringBuilder sb) { for (int i=0; i < depth; i++) sb.append( ); if (node == null) sb.append( null\n ); else { sb.append(node.tostring()); sb.append( \n ); preordertraverse(node.left, depth+1, sb); preordertraverse(node.right, depth+1, sb); } } // preordertraverse() - void preordertraverse(node<e> node, int depth, StringBuilder sb) 17

Height of a Binary Tree height(tree) = ቊ 0 if tree is empty 1 + max(height leftsubtree, height(rightsubtree) a A B C D E F G Height of a Binary Tree 0 if tree is empty height(tree) = ቊ 1 + max(height leftsubtree, height(rightsubtree) public int height() { return height(root); } // height() private int height(node<e> node) { if (node == null) return 0; else return 1 + Math.max(height(node.left), height(node.right)); } // height() 18

- void preordertraverse(node<e> node, int depth, StringBuilder sb) 19