0% found this document useful (0 votes)
2 views3 pages

Debugging Common Java Data Structures

The document contains code snippets for various data structures and algorithms, including binary search, binary tree traversal, stack implementation, and linked list operations. Each section highlights specific errors such as incorrect assignments, missing semicolons, and off-by-one index issues, making it suitable for moderate-level debugging. Overall, it serves as a guide for identifying and correcting common programming mistakes in Java.

Uploaded by

Balajanani .R
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)
2 views3 pages

Debugging Common Java Data Structures

The document contains code snippets for various data structures and algorithms, including binary search, binary tree traversal, stack implementation, and linked list operations. Each section highlights specific errors such as incorrect assignments, missing semicolons, and off-by-one index issues, making it suitable for moderate-level debugging. Overall, it serves as a guide for identifying and correcting common programming mistakes in Java.

Uploaded by

Balajanani .R
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

Simple Binary Search in Array

class BinarySearchDemo {
public static int search(int arr[], int key) {
int low = 0, high = [Link]; // ERROR 1: should be [Link]-1
while(low <= high) {
int mid = (low + high)/2;
if(arr[mid] = key) return mid; // ERROR 2: '=' instead of '=='
else if(arr[mid] < key)
low = mid+1
else
high = mid-1
}
return -1
}

public static void main(String args[]) {


int arr[] = {1,3,5,7,9};
int key = 5;
int result = search(arr,key)
[Link]("Found at: " + result) // ERROR 3: missing
semicolon
}
}

✅ Errors: wrong assignment, missing semicolons, off-by-one in array index — ideal for
moderate-level debugging.

Simple Binary Tree Traversal (Inorder)


class BinaryTree {
Node root;

class Node {
int data;
Node left, right;
Node(int d) {
data = d;
left = right = null;
}
}

void inorder(Node n) {
if(n = null) return; // ERROR: should be '=='
inorder([Link])
[Link]([Link]) // ERROR: missing semicolon
inorder([Link])
}

public static void main(String args[]) {


BinaryTree tree = new BinaryTree();
[Link] = [Link] Node(10)
[Link] = [Link] Node(5)
[Link] = [Link] Node(15)
[Link]([Link]); // ERROR: missing semicolons above
}
}
✅ Errors: wrong comparison, missing semicolons, missing braces.

Simple Stack (Array)


class StackDemo {
int top = -1;
int arr[] = new int[5];

void push(int x) {
if(top >= 5) { // ERROR 1: should be 4
[Link]("Overflow")
return;
}
arr[++top] = x // ERROR 2: missing semicolon
}

int pop() {
if(top < 0) {
[Link]("Underflow") // ERROR 3: missing semicolon
return 0
}
return arr[top--] // ERROR 4: missing semicolon
}

public static void main(String args[]) {


StackDemo s = new Stackdemo(); // ERROR 5: class name mismatch
[Link](10)
[Link](20);
[Link]("Popped: " + [Link]()) // ERROR 6: missing
semicolon above
}
}
Linked List (Insert & Print)

Class LinkedListDemo {

Node head;

Class Node {

Int data;

Node next;

Node(int d) { data=d; next=null; }

Void insert(int d) {

Node n = new Node(d);

If(head=null) { head=n; return; } // ERROR 1: ‘=’ should be ‘==’


Node temp = head;

While([Link]!=null)

Temp=[Link];

[Link]=n

Void printList() {

Node temp=head;

While(temp!=null) {

[Link]([Link]) // ERROR 2: missing semicolon

Temp=[Link]

Public static void main(String args[]) {

LinkedListDemo list=new LinkedListDemo();

[Link](10)

[Link](20);

[Link]()

You might also like