Notes on Assignment 2 Object Delegation
After the BST Constructor Your code will have several classes, most notably the class that represents the entire BST data structure, and the class that is a linked cell (a data element in the tree). The class that is the entire BST will have the methods of the ADT (insert, remove, size, isEmpty, constructors, findMin, etc.) and will have some data fields (counter to keep size, e.g.). The class that is the entire BST will also have a field that points to a data cell that is the root of the tree of data items (or it might be null). aBST root method methods getRoot, etc. insert constructor size method
After aBST.insert(“kappa”) The root field in aBST now points to a new BST_node object with the data stored in it. The left and right child of the root cell are null How did this happen? aBST.insert() was called and the method in the aBST object saw that root was null. So the aBST.insert() method called new BST_node() , put the data “kappa” into the object, and then hooked that object into the aBST.root field. This is a special case… insert when tree has no data in it Special cases are handled directly in the aBST object… no delegation. aBST root method methods getRoot, etc. constructor size 1 data left right null “kappa” insert method
Call to aBST.insert(“omicron”) The root field in aBST still points to the data cell containing “kappa”. The aBST.insert() method runs and notices that size==1. So there is no special case to handle. This means the aBST object will service the method call by calling a method in another object to get the “real work” done. The aBST.insert() method calls aBST.root.insert(“omicron”) and lets the root of the data cell collection figure out where the new cell goes and how it gets linked together with the others. aBST root method methods getRoot, etc. constructor size 1 data left right “kappa” null insert methods null insert method
After aBST.insert(“omicron”) When aBST.root.insert(“omicron”) is called the insert method in the BST_node is run. In this case, it is the BST_node object at the root. This object compares “omicron” to its own data “kappa” and decides that since “omicron” is > “kappa” then “omicron” belongs down in the right child subtree. The right child in the “kappa” cell is null, so the “kappa” cell insert() knows that the new “omicron” cell must be created and hooked in as its right child. The “kappa” cell insert() returns true (success) back to its caller (which was the aBST object insert() ) The aBST.insert() method will bump up size to 2 and then return the true value it received as its own success signal. aBST root method methods getRoot, etc. constructor size data left right “kappa” null insert methods insert method data left right null “omicron” 1 2
After aBST.insert(“lambda”) then aBST.insert(“beta”) data left right “kappa” aBST root method methods getRoot, etc. constructor size 4 data left right null “beta” data left right null “omicron” Insert method data left right null “lambda”