Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science and Engineering

Similar presentations


Presentation on theme: "Computer Science and Engineering"— Presentation transcript:

1 Computer Science and Engineering
Binary Decision Tree Computer Science and Engineering 1/13/2019 B.Ramamurthy

2 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. 1/13/2019 B.Ramamurthy

3 (Zoo) Animal Tree Is it a mammal? no yes Is it an amphibian?
Is it a wild animal? 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 1/13/2019 B.Ramamurthy

4 Simple Guesser: no load/save
1/13/2019 B.Ramamurthy

5 Builder/Guesser with save/no load
AnimalDBBuilder java.io.Serializable BTreeInterface ObjectOutputStream BTree Visitor OutputFile LearnVisitor PlayVisitor 1/13/2019 B.Ramamurthy

6 Guesser with simple load/save
GuesserSimpleLoadSave java.io.Serializable BTreeInterface ObjectOutputStream ObjectInputStream BTree Visitor OutputFile InputFile LearnVisitor PlayVisitor 1/13/2019 B.Ramamurthy

7 Guesser with Load/Save Visitors
OtherApplications GuesserLoadSave java.io.Serializable BTreeInterface ObjectOutputStream ObjectInputStream Visitor BTree OutputFile InputFile LearnVisitor PlayVisitor LoadVisitor SaveVisitor 1/13/2019 B.Ramamurthy

8 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 1/13/2019 B.Ramamurthy

9 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. 1/13/2019 B.Ramamurthy

10 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; 1/13/2019 B.Ramamurthy

11 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); 1/13/2019 B.Ramamurthy

12 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(); 1/13/2019 B.Ramamurthy


Download ppt "Computer Science and Engineering"

Similar presentations


Ads by Google