TREE -NON-LINEAR
DATA STRUCTURE
Tree
is a non-linear data structure that consists of nodes arranged in a
hierarchical order. Each node in a tree has one or more child nodes, and each
child node has one parent node. The topmost node in a tree is called the root
node, and the nodes at the bottom of the tree are called leaf nodes.
In
Java, trees are implemented using the Tree
class. The Tree
class provides methods for traversing the tree, as well as for
accessing and modifying the nodes in the tree.
import java.util.Tree; public class TreeExample { public static void
main(String[] args) {
Tree<Integer> tree = new Tree<>(); tree.add(10); tree.add(5); tree.add(15); tree.add(2); tree.add(7); tree.add(12); tree.add(17);
System.out.println("The root node is: " + tree.getRoot());
// 10
System.out.println("The children of the root node are: " +
tree.getChildren(tree.getRoot())); // [5, 15]
System.out.println("The parent of the node with value 12 is:
" + tree.getParent(12)); // 7 } } |
This code creates a tree
and adds seven nodes to the tree. The code then prints the root node, the
children of the root node, and the parent of the node with value 12.
Here is an explanation
of how the code works:
- The Tree class is imported from the java.util package.
- A new Tree object is created.
- Seven nodes are added to the tree.
- The root node, the children of the root node, and the parent of the
node with value 12 are printed.
GRAPHS
graph
is a non-linear data structure that consists of nodes and edges. The nodes
represent the elements in the graph, and the edges represent the relationships
between the elements.
In
Java, graphs are implemented using the Graph
class. The Graph
class provides methods for traversing the graph, as well as for
accessing and modifying the nodes and edges in the graph.
import
java.util.Graph; public
class GraphExample { public static void main(String[] args) { Graph<String> graph = new
Graph<>(); graph.addVertex("A"); graph.addVertex("B"); graph.addVertex("C"); graph.addVertex("D"); graph.addEdge("A",
"B"); graph.addEdge("A",
"C"); graph.addEdge("B",
"C"); graph.addEdge("C",
"D"); System.out.println("The neighbors of
the node A are: " + graph.getNeighbors("A")); //
["B", "C"] System.out.println("The distance
between the nodes A and C is: " + graph.distance("A",
"C")); // 1 } } |
This code creates a
graph and adds four nodes and four edges to the graph. The code then prints the
neighbors of the node A and the distance between the nodes A and C.
Here is an explanation
of how the code works:
- The Graph class is imported from the java.util package.
- A new Graph object is created.
- Four nodes and four edges are added to the graph.
- The neighbors of the node A and the distance between the nodes A and
C are printed.
HASH TABLES (HASHING)
hash
table is a data structure that maps keys to values. The keys are used to access
the values, and the values can be anything. Hash tables are often used to store
data in a way that allows for quick lookups.
In
Java, hash tables are implemented using the HashMap
class. The HashMap
class provides methods
for adding, removing, and accessing keys and values in the hash table.
import
java.util.HashMap; public
class HashTableExample { public static void main(String[] args) { HashMap<String, Integer> hashTable
= new HashMap<>(); hashTable.put("A", 1); hashTable.put("B", 2); hashTable.put("C", 3); System.out.println(hashTable.get("A"));
// 1
System.out.println(hashTable.get("B")); // 2
System.out.println(hashTable.get("C")); // 3 } } |
This code creates a hash
table and adds three key-value pairs to the hash table. The code then prints
the values associated with the keys "A", "B", and
"C".
Here is an explanation
of how the code works:
- The HashMap class is imported from the java.util package.
- A new HashMap object is created.
- Three key-value pairs are added to the hash table.
- The values associated with the keys "A", "B",
and "C" are printed.
Data
Structure |
Description |
Advantages |
Disadvantages |
Tree |
A hierarchical data structure where each node has at
most one parent node, except for the root node which has no parent node. |
Easy to traverse and search. |
Can be inefficient for storing large amounts of data. |
Graph |
A non-linear data structure where each node can have
multiple parent nodes and multiple child nodes. |
Flexible and can represent complex relationships. |
Can be difficult to traverse and search. |
Hash table |
A data structure that maps keys to values. |
Fast for searching and inserting data. |
Can be inefficient for storing large amounts of data. |
KEY DIFFERENCES BETWEEN
TREES, GRAPHS, AND HASH TABLES
Property |
Tree |
Graph |
Hash table |
Data structure type |
Hierarchical |
Non-linear |
Associative |
Relationship between nodes |
Parent-child |
Adjacent |
Key-value |
Traversal |
Depth-first or
breadth-first |
Breadth-first or
depth-first |
Not applicable |
Searching |
Efficient |
Inefficient |
Efficient |
Inserting data |
Efficient |
Inefficient |
Efficient |
Deleting data |
Efficient |
Inefficient |
Efficient |
Examples of when to use each
data structure:
- Trees: Trees
are often used to represent hierarchical data, such as the file system on
a computer.
- Graphs: Graphs are often used to
represent relationships between entities, such as the social network of friends.
- Hash tables: Hash tables are often used to
store data that is accessed frequently, such as the login credentials for a website.
No comments:
Post a Comment