120 likes | 512 Views
Binary Decision Tree. Computer Science and Engineering. Introduction. Binary Search Tree: Key value at the root partitions the set of data represented in the tree into three disjoint sets: A set with key value at the root A set of keys in the left subtree < key value of the root
E N D
Binary Decision Tree Computer Science and Engineering B.Ramamurthy
Introduction • Binary Search Tree: Key value at the root partitions the set of data represented in the tree into three disjoint sets: • A set with key value at the root • A set of keys in the left subtree < key value of the root • A set of keys in the right subtree > key value of the root • We considered only numerical values, > and < operators. • In a decision tree the criteria for partition is stored in the internal node and the data in the leaf nodes. When the partition is two-way we get a binary decision tree. • Example: • Tree for the popular animal guessing game • The criteria “mammal” could split the zoo animals into two sets, such other criteria could split the two sets further, until the splitting process results in a single animal kind. B.Ramamurthy
Is it an amphibian? Is it a wild animal? (Zoo) Animal Tree Is it a mammal? no yes no yes yes no Frog Parrot Does it have a long neck? Cow yes no Questions with “yes” or “no” answers at the internal nodes; Answers at the leaf nodes. Giraffe Tiger B.Ramamurthy
Simple Guesser: no load/save B.Ramamurthy
Builder/Guesser with save/no load AnimalDBBuilder java.io.Serializable BTreeInterface ObjectOutputStream BTree Visitor OutputFile LearnVisitor PlayVisitor B.Ramamurthy
Guesser with simple load/save GuesserSimpleLoadSave java.io.Serializable BTreeInterface ObjectOutputStream ObjectInputStream BTree Visitor OutputFile InputFile LearnVisitor PlayVisitor B.Ramamurthy
OtherApplications Guesser with Load/Save Visitors GuesserLoadSave java.io.Serializable BTreeInterface ObjectOutputStream ObjectInputStream Visitor BTree OutputFile InputFile LearnVisitor PlayVisitor LoadVisitor SaveVisitor B.Ramamurthy
Load/Save (using Object Streaming) • The capability to store and retrieve Java objects is essential to building persistence and streaming into application. • Writing to an Object Stream • Reading from an Object Stream • Exception Handling • Example: See the code in the BTree example B.Ramamurthy
Persistence • The values of the data structures in a program are lost when the program terminates. • If you want the data to exist after program termination you need to store it in a file or in a database. • This capability of data is known as persistence. B.Ramamurthy
How to use Serialization? 1. import java.io.*; 2. Specify the class whose instances need to be stored/loaded as implementing java.io.Serializable interface. 3. Use writeObject and readObject methods whose header are as given below: void writeObject (Object obj) throws IOException; Object readObject() throws ClassNotFoundException, IOException; B.Ramamurthy
Writing to an Object Stream // serialize various objects into a file FileOutputStream f = new FileOutputStream(“tmp”); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject(“Today”); s.writeObject(new Date()); BTree t = new BTree(“MyName”, null, null); s.writeObject(t); B.Ramamurthy
Reading from an Object Stream // Deserialize a objects from a file FileInputStream inf = new FileInputStream(“tmp”); ObjectInputStream s1 = new ObjectInputStream(inf); //read the Object and cast to retrieve the // actual object String = (String)s1.readObject(); Data date = (Date)s1.readObject(); BTree animalDB = (BTree)s1.readObject(); B.Ramamurthy