0% found this document useful (0 votes)
4 views5 pages

HTML TREE 04 - Hands-On - Activity - 1

The document outlines a hands-on activity for students to create and manipulate an HTML tree using Java methods in NetBeans IDE. It includes a step-by-step procedure for setting up the project, creating a JTree, and enumerating various tree properties such as root nodes, parent nodes, and subtrees. Additionally, it provides a sample Java code implementation for the exercise.

Uploaded by

sai.romero.coi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

HTML TREE 04 - Hands-On - Activity - 1

The document outlines a hands-on activity for students to create and manipulate an HTML tree using Java methods in NetBeans IDE. It includes a step-by-step procedure for setting up the project, creating a JTree, and enumerating various tree properties such as root nodes, parent nodes, and subtrees. Additionally, it provides a sample Java code implementation for the exercise.

Uploaded by

sai.romero.coi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Hands-on Activity

HTML Tree
Objective:

At the end of the exercise, the students should be able to:


 Use Java methods to create a tree and manipulate its nodes.
Software Requirements:
 NetBeans IDE
 Java Development Kit (JDK) 8
Procedure:
1. Create a folder named LastName_FirstName in your local drive. (ex. Reyes_Mark)
2. Using NetBeans, create a Java project named HtmlTree. Set the project location to your
own folder.
Part 1 (Week 8)
3. Create and display a JTree based on the given tree figure. Refer to 06 Handout 1 for
sample codes.

Expected Output:
Part 2 (Week 9)
4. Using Java methods and println(), enumerate the following:
4.1. Root node
4.2. Parent nodes
4.3. Siblings
4.4. One-level subtrees
4.5. Nodes per level
4.6. Depth
4.7. Degree of each one-level subtree
4.8.List of nodes based on breadth-first, preorder, and postorder

CODE:

package com.mycompany.htmltree;

import java.util.Collections;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class HtmlTree {


JTree tree;
JFrame frame;

public HtmlTree() {
frame = new JFrame();

DefaultMutableTreeNode html = new DefaultMutableTreeNode("html");


DefaultMutableTreeNode head = new DefaultMutableTreeNode("head");
DefaultMutableTreeNode body = new DefaultMutableTreeNode("body");
html.add(head);
html.add(body);
DefaultMutableTreeNode meta = new DefaultMutableTreeNode("meta");
DefaultMutableTreeNode title = new DefaultMutableTreeNode("title");
head.add(meta);
head.add(title);
DefaultMutableTreeNode ul = new DefaultMutableTreeNode("ul");
DefaultMutableTreeNode h1 = new DefaultMutableTreeNode("h1");
DefaultMutableTreeNode h2 = new DefaultMutableTreeNode("h2");
body.add(ul);
body.add(h1);
body.add(h2);
DefaultMutableTreeNode li1 = new DefaultMutableTreeNode("li");
DefaultMutableTreeNode li2 = new DefaultMutableTreeNode("li");
ul.add(li1);
ul.add(li2);
DefaultMutableTreeNode a = new DefaultMutableTreeNode("a");
h2.add(a);

tree = new JTree(html);

DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();


System.out.print("Root Node: " + root.toString());
System.out.println(" \n");
System.out.print("Parent Nodes: ");
parents(root);
System.out.println(" \n");

System.out.print("Siblings: ");
siblings(root);
System.out.println(" \n");

System.out.print("One Level Subtrees: ");


subtrees(root);
System.out.println(" \n");

System.out.print("Nodes per Level: ");


levels(root);
System.out.println(" \n");

System.out.print("Depth: " + root.getDepth());


System.out.println(" \n");

System.out.print("Degree for each one level subtree: ");


degrees(root);
System.out.println(" \n");

System.out.println("Breadth-first: " + Collections.list(root.breadthFirstEnumeration()));

System.out.println("Pre-order: " + Collections.list(root.preorderEnumeration()));

System.out.println("Postorder: " + Collections.list(root.postorderEnumeration()));

frame.add(tree);
frame.setTitle("JTree Example");
frame.setSize(300, 300);
frame.setVisible(true);
}

public static void main(String[] args) {


new HtmlTree();
}

public static void parents(DefaultMutableTreeNode node) {


if (!node.isLeaf()) {
System.out.print(node.toString() + ", ");
}
for (int i = 0; i < node.getChildCount(); i++) {
parents((DefaultMutableTreeNode) node.getChildAt(i));

if (node.isLeaf()) {
return;
}
}
}

public static void siblings(DefaultMutableTreeNode node) {


if (node.getChildCount() > 1) {
System.out.print(Collections.list(node.children()) + ", ");
}
for (int i = 0; i < node.getChildCount(); i++) {
siblings((DefaultMutableTreeNode) node.getChildAt(i));

if (node.isLeaf()) {
return;
}
}
}

public static void subtrees(DefaultMutableTreeNode node) {


if (!node.isLeaf()) {
System.out.print(node.toString() + " - " + Collections.list(node.children()) + ",");
}
for (int i = 0; i < node.getChildCount(); i++) {
subtrees((DefaultMutableTreeNode) node.getChildAt(i));

if (node.isLeaf()) {
return;
}
}
}

public static void levels(DefaultMutableTreeNode node) {


int level = node.getLevel();
System.out.print(node.toString() + " - " + level + ", ");
for (int i = 0; i < node.getChildCount(); i++) {
levels((DefaultMutableTreeNode) node.getChildAt(i));

if (node.isLeaf()) {
return;
}
}
}

public static void degrees(DefaultMutableTreeNode node) {


if (!node.isLeaf()) {
System.out.print(node.toString() + " - " + node.getChildCount());
}
for (int i = 0; i < node.getChildCount(); i++) {
degrees((DefaultMutableTreeNode) node.getChildAt(i));

if (node.isLeaf()) {
return;
}
}
}
}
OUTPUT:

You might also like