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