Tree Data Structure: Design and Implementation in
Tree Data Structure: Design and Implementation in
w Why Trees?
w Trees
w Tree Terminology
w Difference between Tree & Binary
Trees
w Applications of Tree
w Huffman Coding Tree
w One Recursive Tree Algorithm
Implementation
9-2
Storing Many Objects
2 3
edges 9-4
First Some Definitions
•
Trees have layers of nodes,
•
B C D where some are higher,
others are lower.
•
w
•
E F
•
— Root node is A
— A's children are B, C, and D
— E, F, and D are leaves
— Length of path from A to E is
9-6 2
Some tree terminology
• Root
•
Object
•
• children of root
•
Number Throwable OutputStream
• grand children of root
•
w
RuntimeException
9-9
Applications of trees
w File Systems
— Hierarchical files systems include
Unix and DOS
— In DOS, each \ represents an edge
(In Unix, it's /)
— Each directory is a file with a list
of all its children
w Store large volumes of data
— data can be quickly inserted,
removed, and found
w Data structure used in a variety of
situations
— implement data vase management systems9-10
— compilers: expression tree, symbol tree
Huffman Coding Tree
windowing toolkit
•
import java.awt.event.*;
• \\ importing awt.event
•
import javax.swing.*;
• \\ importing javax.swing
•
swing.tree package
•
9-12
Creating Tree1 class
•{
•public static void main(String args[])
visibility
•
9-13
Creating Nodes…
•}
•public Tree1(String title) \\ creating class
name
•
•
•
9-14
Creating Style to Nodes….
DefaultMutableTreeNode ("Information
•
Technology");
•
root.add(style);
• \\ add sub root or leaf node.
•
Container contentPane=getContentPane();\\
•
w 3 Constructors
— two set some data fields to null
(left and right)
w The data fields are private
— Like LinkNode, methods in the
enclosing class can reference9-16
Memory linked structure diagram
Root
Engineering
“Computer Science "
‘0’ ‘0’
9-17
•import java.awt.*;
•import java.awt.event.*;
•import javax.swing.*;
•import javax.swing.tree.*;
•
•{
•frame.setSize(200,200);
•frame.setVisible(true);
•}
•{
•setTitle(title);
•DefaultMutableTreeNode root=new
DefaultMutableTreeNode ("Engineering");
•DefaultMutableTreeNode style=new
DefaultMutableTreeNode ("Information
Technology");
•root.add(style);
9-18
•style=new
DefaultMutableTreeNode("Electronics")
;
•root.add(style);
•style=new DefaultMutableTreeNode
("Computer Science");
•root.add(style);
•style=new DefaultMutableTreeNode
("Mechanical");
•root.add(style);
•style=new DefaultMutableTreeNode
("Electrical");
•root.add(style);
•style=new DefaultMutableTreeNode
("Sound");
•root.add(style);
•contentPane.add(new JScrollPane(jt));
•}
•}
9-19
Any Questions ?
•
9-20
Thank you
9-21