SNS

About Sui Name Service Introducing the Sui Name Service (SuiNS). SuiNS is built as an open decentralized name service on the Sui blockchain. Our first product is the .sui domain name extension…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Trees

Group members: Pelaez, Atendido, Mariano

This article consists of the topic “Trees” on our data structure subject. The topic is very broad, but we have arranged some key information for you to understand “Trees” better and in order for the program we have created to be simple in understanding terms.

A tree represents a hierarchical nature of a structure in a graphical form. It consists of elements or nodes, with each node linked to its successors.

The top of tree is called its root. The links from a node to its successors are called branches, edges, lines, or paths. The successors of a node are called its child nodes or children. The predecessor of a node is called its parent.

The best example of tree is the computer’s file system: C:\Users\bpena\Desktop\TreeDemo.java

A tree is considered a binary tree if all its nodes have two (2) child nodes at most. Each node in a tree has exactly one (1) parent except for the root node, which has no parent. Nodes that have the same parent are siblings.

A node that has no child nodes is a leaf node or external node. Nodes that have children are known as internal nodes.

A tree within a tree is considered a subtree.

The level of a node is a measure of its distance from the root.

The depth of the tree is its highest level.

The degree is the number of child nodes in a subtree.

Example:

Example of a tree

Traversal is the process of visiting all the nodes in a specific order. The following are the different traversal types:

Traversal tree

Breadth-First or Level Order: Nodes are visited by level 1, 2, 3, 4, 5.

Inorder (Left, Root, Right): 4, 2, 5, 1, 3
Start with the bottommost left subtree. Once the root in Level 0 is visited, proceed to the bottommost right subtree.

Preoder (Root, Left, Right): 1, 2, 4, 5, 3
Start with the root in Level 0 then continue with the left subtree.

Postorder (Left, Right, Root): 4, 5, 2, 3, 1
Start with the bottommost left subtree then proceed to the other subtrees. The root in Level 0 is the last node visited

The JTree is a Java Swing component that displays a set of hierarchical data as an outline. It is included in the javax.swing package.

The Java class, DefaultMutableTreeNode, is used to represent a general-purpose node in a tree data structure. It is included in the javax.swing.tree package.

The add() method removes a node from its parent and makes it a child of another node by adding it to the end of the node’s child array.

Other Java methods used in retrieving values from a tree are:

Image of a clock

About our program

We have created a program that would show you the sequence of numbers when you have a clock but horizontally. This program will use a java tree that will show you the sequence of the clock using the transverse recursion tree.

Clock Horizon upper part

We would be using the upper part of the clock when you cut it horizontally, we also created a tree referring to it for you to understand it better.

The numbers inside the node represents the number of hours inside a clock, if you put it into a tree it would look like this.

Program:
This is the code for the program that we have created.

Code:

Output:

In order for you to comprehend the code, we will explain how each line adds to the program’s output.

The class Node represents the name of the class while the variables like int num , Node left and Node right represents there own type of variables which will be used at a later part of the code. (Take note that this variables will playu a huge role in the code itself).

While the public Node(int num)represents the parameter of the Node() which states that the int num earlier is the same as the int num inside the parameter using the this.num = num; . The left = null; and the right = null; represents the default type of the nodes of left and right.

The public class WallClock {} represents the name of the main class. The Node root; is the same as the variable earlier like Node left; and Node right; but we putted it here because it needs to be read by the main class later on. The public void traverseRecursionTree(Node node) {} is a parameter with no return type, this shows that it needs a node type parameter in order for it to work.

The if (node != null) {} is an if statement that sets if the node is not equal to null it would then read the furthest left side of the node using the traverseRecursionTree(node.left); . After that it would print the integer of the variable num with a space so it would look better on the output. Then it would read the node on the right side of the written tree if the nodes that it reads wasn’t a null anymore using the traverseRecursionTree(node.right);

This part of the code make sure that the nodes that has been read won’t be repeated.

The public static void main(String[] args) {} is the main method of the program while the WallClock javaTree = new WallClock(); is the code to create an empty tree with the name of javaTree. The similar codes that you will see in the middle (ex. javaTree.root.left.left = new Node(10);) would be the declaration of the integers inside the nodes of the tree. The position of the node depends on how far you will input the position using the left and right words. Take note that there could only be one root node in every tree.

The System.out.print(“Clockface : “); represents that the output would show the clockwise position of the horizontal clock from 9–3. While the javaTree.traverseRecursionTree(javaTree.root); calls the method from the traverseRecursionTree() from the javaTree class inside the program.

Notice that we use the javaTree often because it calls out where the program should find the given commands for the user to take.

As a group, we have concluded that trees help the user to store data in a hierarchy form so that it would be convenient for the user when he/she is managing some things in a formal way. This is helpful because the tree itself would help the user form a type of hierarchy based on the objects the user has inputted inside the tree.

You could find it similar to a graph since the tree shows a form of information that is basically appealing and is simply understandable especially when showing hierarchy on things. Therefore it would be helpful for us in the future since we can apply it to certain programs and make it easier for us to implement trees. Thankyou for your time

Add a comment

Related posts:

Minority marriages Of Bangladesh

Various types of wedding ceremonies have been practiced among ethnic minorities for a long time. The distinctive identity of this community is also found through their marriage customs. There are…

Day Two

Music can inspire. Music makes you feel so happy and helps you connect with others. It puts words into your mouth that you’re unable to share. With technology today, we’re constantly surrounded by…

75 quotes from 60 pages of notes from 25 meetings from 18 people from 10 weeks of Techstars Chicago.

I tried my best to carry my notebook with me to every meeting and every session that I was able to attend. (And my friend Rachael made sure I went to every session I was in the building for) I’m now…