I need to write a console application that maintains a collection of CDs including title, artist, record company etc..
This data needs to be stored in a Binary Tree, and I can choose whatever serach key I wish.
I have written the classes for Binary Tree to store Integer Objects, however I cannot get my head around how I can implement this with a "CD" Object as such..
Below is my Binary Tree class I have..
Any advice would be greatly appreciated..Code:public class BTNode { private Object data; private BTNode left, right; public BTNode(Object initialData, BTNode initialLeft, BTNode initialRight) { data = initialData; left = initialLeft; right = initialRight; } public BTNode() { this(null, null, null); } public BTNode(Object newdata) { this(newdata, null, null); } //NODE methods public Object getData() //return and set data in node {return data;} public void setData(Object data) {this.data = data; } public BTNode getLeft() //return and set left child {return left; } public void setLeft(BTNode left) {this.left = left; } public BTNode getRight() //return and set right child {return right; } public void setRight(BTNode right) {this.right = right; } public boolean isLeaf() //return true if this is leaf node {return (left == null)&&(right == null); } // TREE Methods - for tree starting at this node // get specific data from a tree public Object getLeftmostData() { if(left == null) return data; else return left.getLeftmostData(); } public Object getRightmostData() { if(right == null) return data; else return right.getRightmostData(); } // print data in a tree public void inorderPrint() { if (left != null) left.inorderPrint( ); System.out.print(data+"\t"); if (right != null) right.inorderPrint( ); } public void preorderPrint() { System.out.print(data+"\t"); if (left != null) left.preorderPrint( ); if (right != null) right.preorderPrint( ); } public void postorderPrint() { if (left != null) left.postorderPrint( ); if (right != null) right.postorderPrint( ); System.out.print(data+"\t"); } // pretty print a tree //public void print(int depth); //Interface for Nodes in Binary Tree (3) //removing nodes from a tree //returns reference to tree after removal of node public BTNode removeLeftmost() { if (left == null) // we are as deep as we can get return right; // remove this node by returning right subt ree else { // keep going down recursively left = left.removeLeftmost( ); // when done, return node that act ivated this // method as root of t ree return this; }} public BTNode removeRightmost() { if (right == null) // we are as deep as we can get return left; // remove this node by returning left subt ree else { // keep going down recursively right = right.removeRightmost( ); // when done, return node that activated this // method as root of tree return this; }} //returns number of nodes in tree public static long treeHeight(BTNode root) { if (root == null) return -1; else return 1 + Math.max(treeHeight(root.left), treeHeight(root.right)); } public static long treeSize(BTNode root) { if (root == null) return 0; else return 1 + treeSize(root.left) + treeSize(root.right); } //copying a tree: returns reference to root of copy // public BTNode treeCopy(BTNode root); public static BTNode insertNode(BTNode root, int key){ if (root == null) // null tree, so create node at root return new BTNode(new Integer(key)); Integer data_element = (Integer) root.data; if (key <= data_element.intValue()) root.setLeft(insertNode(root.left, key)); else root.setRight(insertNode(root.right, key)); return root; } public static BTNode findNode(BTNode root,int key){ if (root == null) return null; // null tree Integer data_element = (Integer) root.data; if (key == data_element.intValue()) return root; else if (key <= data_element.intValue()) return findNode(root.left, key); else return findNode(root.right, key); } public static BTNode removeNode(BTNode root, int key){ if (root == null) return null; Integer data_element = (Integer) root.data; if (key < data_element.intValue()) root.setLeft(removeNode(root.left, key)); else if (key > data_element.intValue()) root.setRight(removeNode(root.right,key)); else { // found it if (root.right == null) root = root.left; else if (root.left == null) root = root.right; else { //two children Integer temp = (Integer) root.left.getRightmostData(); root.setData(temp); root.setLeft(root.left.removeRightmost()); } } return root; } public static BTNode copyTree(BTNode root){ if (root == null) return null; BTNode left = copyTree(root.left); BTNode right = copyTree(root.right); return new BTNode(root.data,left,right); } }
Thankyou


Reply With Quote


Bookmarks