CIS 265 Lecture Notes Binary Trees V. Matos DRIVER1. package csu.matos; public class Driver1 {

Similar documents
CIS265 Homework7 A (partial) solution showing creation/traversal of an expression-tree made from a a valid arithmetical expression

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

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

Chapter 5 - A Simple Dynamic Linked List (Holding Strings)

Introduction to Algorithms and Data Structures

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

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

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

(Constructor) public A (int n){ for (int i = 0; i < n; i++) { new A(i); } System.out.println("*"); }

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CSC 321: Data Structures. Fall 2016

CmpSci 187: Programming with Data Structures Spring 2015

CSC 321: Data Structures. Fall 2012

PASS4TEST IT 인증시험덤프전문사이트

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

Binary trees. Binary trees. Binary trees

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

Data Structures and Algorithms

IMPLEMENTING BINARY TREES

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

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

1 import java.util.*; 2 3 // implementing hash tables as an array of linked lists 4 // and using it to check whether two sequencs are permutations of

Building Java Programs

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

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

/department of mathematics and computer science 1/58

CMSC 132, Object-Oriented Programming II Summer Lecture 12

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

Do not turn over this examination paper until instructed to do so. Answer all questions.

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

CS302 - Data Structures using C++

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Chapter 20: Binary Trees

Course Project Hospital Information Management

ITI Introduction to Computing II

CS/ENGRD2110: Final Exam

ITI Introduction to Computing II

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

Distributed Systems Recitation 1. Tamim Jabban

Facebook. / \ / \ / \ Accenture Nintendo

Assignment-1 Final Code. Student.java

Fundamental Java Methods

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

CMSC 132, Object-Oriented Programming II Summer Lecture 13

1. Binary Tree Traversal. 2. Binary Search Tree. 1 of 12. Consider the following tree: Pre-order: In-order: Post-order:

CMP-326 Total 120 Points Final Fall 2012

CMSC 132: Object-Oriented Programming II. Inheritance

Abstract Classes and Interfaces

King Saud University College of Computer and Information Systems Department of Computer Science CSC 113: Java Programming-II, Spring 2016

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #4. Review: Methods / Code

Distributed Systems Recitation 1. Tamim Jabban

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

DM537/DM550 Object-Oriented Programming. Jan Baumbach

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

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

CS24 Week 8 Lecture 1

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

InfiniteGraph Manual 1

1 import java.util.*; 2 3 // implementing hash table as an array of linked lists 4 // and using it to print the unique elements of an array 5 6 class

Computer Science II Fall 2009

Data Structures in Java

CS 302 Week 9. Jim Williams

The class Object. Lecture CS1122 Summer 2008

BM214E Object Oriented Programming Lecture 6. Classes and Objects

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

Collections Algorithms

CSCS-200 Data Structure and Algorithms. Lecture

1. Introduction. Lecture Content

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Sample Questions for Midterm Exam 2

AVL Trees. CSE260, Computer Science B: Honors Stony Brook University

1 import java.util.*; 2 3 // implementing hash table as an array of linked lists 4 // and constructing a union of two linked lists 5 // the union

Abstract class & Interface

MYBATIS - ANNOTATIONS

DM550 Introduction to Programming part 2. Jan Baumbach.

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

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

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Introduction to Java

Java Classes, Inheritance, and Interfaces

PROGRAMMING FUNDAMENTALS

Oracle 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam

DM503 Programming B. Peter Schneider-Kamp.

Introduction to Programming Using Java (98-388)

Solutions to Sample JAC444 Midterm Test

COE318 Lecture Notes Week 9 (Oct 31, 2011)

The code XXX1XXX, XXX2XXX, XXX3XXX represents pieces of code that are missing from the method that are needed to make it work properly.

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

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

The Binary Search Tree ADT

Programming Languages and Techniques (CIS120)

Back To LinkedLists 1. 3 public Node left; 4 public Node right; 6 public Node(int data, Node left, Node right) {

Introducing Generics

Transcription:

CIS 265 Lecture Notes Binary Trees V. Matos DRIVER1 public class Driver1 { /** * GOAL: create a simple binary tree using custom-made linked nodes * nodes carry Integer numbers */ public static void main(string[] args) { Node<Integer> n1 = new Node<Integer> (58); Node<Integer> n2 = new Node<Integer> (77); Node<Integer> n3 = new Node<Integer> (25); Node<Integer> n4 = new Node<Integer> (12); Node<Integer> n5 = new Node<Integer> (55); Node<Integer> n6 = new Node<Integer> (85); Node<Integer> n7 = new Node<Integer> (80); Node<Integer> n8 = new Node<Integer> (90); Node<Integer> n9 = new Node<Integer> (40); // System.out.println ( n1.shownode() ); // System.out.println ( n2.shownode() ); // System.out.println ( n3.shownode() ); //main Tree<Integer> tree = new Tree<Integer>(); tree.add(n1); tree.add(n2); tree.add(n3); tree.add(n4); tree.add(n5); tree.add(n6); tree.add(n7); tree.add(n8); tree.add(n9); tree.preorder(); Node<Integer> ptr = tree.findnode(77); System.out.println ( ptr.getdata() + " is at: " + ptr ); tree.bfs(); System.out.println ( "Rightmost " + tree.findrightmostleft(tree.getroot()).shownode() ); System.out.println ( " Rightmost 77 is " + tree.findrightmostleft(ptr) ); //tree.delete(tree.getroot()); ptr = tree.findnode(85); tree.delete(ptr); tree.bfs();

Node Class public class Node<E extends Comparable<E>> implements Comparable<Node<E>> { E data; Node<E> left; Node<E> right; public Node(E data) { this.data = data; this.left = null; this.right = null; public E getdata() { return data; public void setdata(e data) { this.data = data; public Node<E> getleft() { return left; public void setleft(node<e> left) { this.left = left; public Node<E> getright() { return right; public void setright(node<e> right) { this.right = right; // ------------------------------------------------ public String shownode() { return "\n\tdata: " + this.getdata() + "\n\tlocation: " + this + "\n\t Left: " + this.getleft() + "\n\t Right: " + this.getright(); public String ShowNodeLess() { return "\tdata: " + this.getdata() + "\tloc: " + nodeaddress(this) + "\t Left: " + nodeaddress(this.getleft()) + "\t Right: " + nodeaddress(this.getright()); @Override public int compareto(node<e> othernode) { return this.compareto(othernode); private String nodeaddress(node<e> ptr) { if (ptr == null) return "null "; String result = ptr.tostring(); return result.substring(result.indexof('@'));

Tree Class import java.util.linkedlist; import java.util.queue; public class Tree<E extends Comparable<E>> { Node<E> root; //------------------------------------------ public Tree () { root = null; //------------------------------------------ public Node<E> getroot() { return root; public void setroot(node<e> root) { this.root = root; // ---------------------------------------------------------- public Node<E> findnode ( E data){ // locate a node containing given E-type data Node<E> current = root; while ( current!= null ){ if ( current.getdata().compareto(data) == 0) return current; if ( current.getdata().compareto(data) > 0) current = current.getleft(); else current = current.getright(); return current; // ---------------------------------------------------------- public void add (Node<E> newnode){ if ( root == null ){ root = newnode; return; Node<E> current = root; Node<E> parent = null; boolean leftchild = false; while ( current!= null ){ if ( current.getdata().compareto(newnode.getdata()) > 0){ parent = current; current = current.getleft(); leftchild = true; parent = current; current = current.getright(); leftchild = false; if ( leftchild){ parent.setleft(newnode);

parent.setright(newnode); // ------------------------------------------ public void preorder() { preorder(root); public void preorder(node<e> ptr) { if (ptr == null) return; System.out.println ( ptr.shownodeless() ); preorder(ptr.getleft()); preorder(ptr.getright()); //------------------------------------------ public void bfs() { Queue<Node<E>> queue = new LinkedList<Node<E> >(); queue.clear(); queue.add(root); Node<E> current; while (! queue.isempty() ){ current = queue.remove(); if ( current!= null) { System.out.println ( current.shownodeless() ); queue.add(current.getleft()); queue.add(current.getright()); //bfs // ---------------------------------------------------- public Node<E> findrightmostleft(node<e> ptr) { if ( ptr == null ) return null; Node<E> current = ptr.getleft(); Node<E> parent = ptr; while ( current!= null && current.getright()!= null ){ System.out.println ("debug current" + current.shownodeless() + "\tparent:" + parent.shownodeless()); parent = current; current = current.getright(); return current; private Node<E> findparent (Node<E> childnode){ Node<E> ptr = root; Node<E> parent = null; while ( ptr!= null ){ if ( ptr == childnode) break; parent = ptr; if ( ptr.getdata().compareto(childnode.getdata())> 0) ptr = ptr.getleft(); else ptr = ptr.getright(); return parent;

public void delete(node<e> node2delete) { Node<E> rightmost = findrightmostleft(node2delete); Node<E> parent; if ( rightmost!= null ){ parent = findparent( rightmost ); node2delete.setdata( rightmost.getdata() ); parent.setright( rightmost.getleft() ); parent = findparent( node2delete ); if ( parent.getleft() == node2delete ){ parent.setleft( node2delete.getright() ); parent.setright( node2delete.getright() ); System.gc(); Person class public class Person implements Comparable<Person> { String name; int phone; public Person(String name, int phone) { super(); this.name = name; this.phone = phone; public String getname() { return name; public void setname(string name) { this.name = name; public int getphone() { return phone; public void setphone(int phone) { this.phone = phone; /////////////////////////////////////// public String tostring() { return " Name:" + name + " Phone:" + phone; @Override public int compareto(person otherperson) { if (this.phone == otherperson.getphone()) return 0; else if (this.phone > otherperson.getphone()) return 1; else return -1;

Driver2 public class Driver2 { /** * Goal: testing the custom-made Tree class holding Person nodes */ public static void main(string[] args) { Tree<Person> tree = new Tree<Person>(); Person p1 = new Person("AAA", 555); System.out.println ( "p1 >>> " + p1); tree.add(new Node<Person>( new Person("AAA", 555) )); tree.add(new Node<Person>( new Person("BBB", 333) )); tree.add(new Node<Person>( new Person("CCC", 777) )); tree.add(new Node<Person>( new Person("DDD", 222) )); tree.add(new Node<Person>( new Person("EEE", 111) )); tree.add(new Node<Person>( new Person("FFF", 888) )); tree.add(new Node<Person>( new Person("GGG", 444) )); tree.preorder(); Person p2 = new Person("CCC", 777); Node<Person> nodeptr = tree.findnode( p2 ); System.out.println( " p2: " + p2.tostring() + "\n p2 is at: " + nodeptr); //tree.delete( new Node<Person> (new Person("AAA", 555)) ); tree.delete ( nodeptr ); tree.preorder();

Console Running Driver1 Data: 58 Loc: @2f9ee1ac Left: @67f1fba0 Right: @3fbefab0 Data: 25 Loc: @67f1fba0 Left: @133c5982 Right: @5f186fab Data: 12 Loc: @133c5982 Left: null Right: null Data: 55 Loc: @5f186fab Left: @3d4b7453 Right: null Data: 40 Loc: @3d4b7453 Left: null Right: null Data: 77 Loc: @3fbefab0 Left: null Right: @24c21495 Data: 85 Loc: @24c21495 Left: @41d5550d Right: @1cc2ea3f Data: 80 Loc: @41d5550d Left: null Right: null Data: 90 Loc: @1cc2ea3f Left: null Right: null 77 is at: csu.matos.node@3fbefab0 Data: 58 Loc: @2f9ee1ac Left: @67f1fba0 Right: @3fbefab0 Data: 25 Loc: @67f1fba0 Left: @133c5982 Right: @5f186fab Data: 77 Loc: @3fbefab0 Left: null Right: @24c21495 Data: 12 Loc: @133c5982 Left: null Right: null Data: 55 Loc: @5f186fab Left: @3d4b7453 Right: null Data: 85 Loc: @24c21495 Left: @41d5550d Right: @1cc2ea3f Data: 40 Loc: @3d4b7453 Left: null Right: null Data: 80 Loc: @41d5550d Left: null Right: null Data: 90 Loc: @1cc2ea3f Left: null Right: null debug current Data: 25 Loc: @67f1fba0 Left: @133c5982 Right: @5f186fab parent: Data: 58 Loc: @2f9ee1ac Left: @67f1fba0 Right: @3fbefab0 Rightmost Data: 55 Location: csu.matos.node@5f186fab Left: csu.matos.node@3d4b7453 Right: null Rightmost 77 is null Data: 58 Loc: @2f9ee1ac Left: @67f1fba0 Right: @3fbefab0 Data: 25 Loc: @67f1fba0 Left: @133c5982 Right: @5f186fab Data: 77 Loc: @3fbefab0 Left: null Right: @24c21495 Data: 12 Loc: @133c5982 Left: null Right: null Data: 55 Loc: @5f186fab Left: @3d4b7453 Right: null Data: 80 Loc: @24c21495 Left: @41d5550d Right: null Data: 40 Loc: @3d4b7453 Left: null Right: null Data: 80 Loc: @41d5550d Left: null Right: null Console Running Driver2 p1 >>> Name:AAA Phone:555 Data: Name:AAA Phone:555 Loc: @67f1fba0 Left: @3fbefab0 Right: @133c5982 Data: Name:BBB Phone:333 Loc: @3fbefab0 Left: @5f186fab Right: @3d4b7453 Data: Name:DDD Phone:222 Loc: @5f186fab Left: @24c21495 Right: null Data: Name:EEE Phone:111 Loc: @24c21495 Left: null Right: null Data: Name:GGG Phone:444 Loc: @3d4b7453 Left: null Right: null Data: Name:CCC Phone:777 Loc: @133c5982 Left: null Right: @41d5550d Data: Name:FFF Phone:888 Loc: @41d5550d Left: null Right: null p2: Name:CCC Phone:777 p2 is at: csu.matos.node@133c5982 Data: Name:AAA Phone:555 Loc: @67f1fba0 Left: @3fbefab0 Right: @41d5550d Data: Name:BBB Phone:333 Loc: @3fbefab0 Left: @5f186fab Right: @3d4b7453 Data: Name:DDD Phone:222 Loc: @5f186fab Left: @24c21495 Right: null Data: Name:EEE Phone:111 Loc: @24c21495 Left: null Right: null Data: Name:GGG Phone:444 Loc: @3d4b7453 Left: null Right: null Data: Name:FFF Phone:888 Loc: @41d5550d Left: null Right: null