0% found this document useful (0 votes)
10 views

What Are Data Structures

Data structures are formats for organizing and storing data to enable efficient access and management. They are categorized into primitive data structures, like integers and booleans, and non-primitive data structures, such as arrays and trees. The choice of data structure significantly impacts the efficiency of algorithms for operations like searching and sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

What Are Data Structures

Data structures are formats for organizing and storing data to enable efficient access and management. They are categorized into primitive data structures, like integers and booleans, and non-primitive data structures, such as arrays and trees. The choice of data structure significantly impacts the efficiency of algorithms for operations like searching and sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

What Are Data Structures?

Data structures are specialized formats for organizing and storing data in a way that enables efficient access,
modification, and management. In other words, a data structure is a way to organize and store data so that it can be
accessed and worked with efficiently.

The choice of a data structure plays a crucial role in how effectively an algorithm performs. It determines the
efficiency of operations such as searching, insertion, deletion, and sorting.

Types of Data Structures

Data structures can be broadly categorized into two types:

1. Primitive Data Structures:

o These are the most basic data types provided by most programming languages. They represent the
simplest form of data.

o Examples: Integers, floats, characters, and booleans.

2. Non-Primitive Data Structures:

o These are more complex and are built using primitive data structures. They are used to store
collections of data and are designed to organize data efficiently for specific applications.

o Examples: Arrays, linked lists, stacks, queues, trees, graphs, hash tables, etc.
class BinarySearchTree { // Search for a value in the BST
class Node { public boolean search(int data) {
int data; return searchRec(root, data);
Node left, right; }

public Node(int data) { // Recursive search function


this.data = data; private boolean searchRec(Node root, int data) {
left = right = null; if (root == null) {
} return false;
} }

private Node root; if (data == root.data) {


return true;
public BinarySearchTree() { }
root = null;
} if (data < root.data) {
return searchRec(root.left, data);
// Insert a node into the BST }
public void insert(int data) {
root = insertRec(root, data); return searchRec(root.right, data);
} }

// Recursive insert function // Main method to test the BinarySearchTree class


private Node insertRec(Node root, int data) { public static void main(String[] args) {
if (root == null) { BinarySearchTree bst = new BinarySearchTree();
root = new Node(data);
return root; // Insert values into the BST
} bst.insert(50);
bst.insert(30);
if (data < root.data) { bst.insert(20);
root.left = insertRec(root.left, data); bst.insert(40);
} else if (data > root.data) { bst.insert(70);
root.right = insertRec(root.right, data); bst.insert(60);
} bst.insert(80);

return root; // Print the inorder traversal (sorted order)


} System.out.println("Inorder traversal:");
bst.inorder();
// Inorder Traversal (Left, Root, Right)
public void inorder() { // Search for a value in the BST
inorderRec(root); System.out.println("\nSearch 40: " +
} bst.search(40));
System.out.println("Search 25: " + bst.search(25));
// Recursive inorder traversal }
private void inorderRec(Node root) { }
if (root != null) {
inorderRec(root.left);
System.out.print(root.data + " ");
inorderRec(root.right);
}
}

You might also like