More

    [CODE] LINKED LIST, TREE AND GRAPH NODE DEFINITIONS

    Basic building blocks of a linked data structure like linked list, trees, and graphs are nodes. All these data structures are created by connecting these nodes. Here is a summary of nodes and their most commonly used structure, defined in Java. Structure should be similar in other programming languages.

    LINKED LIST NODE

    public class ListNode {

        int data;

        ListNode next;

    }

    Variable next will point to the next node in a linked list.

    BINARY TREE NODE

    public class BTreeNode {

        int data;

        BTreeNode left, right;

    }

    Every node will have pointers to its left and right child.

    TREE NODE

    import java.util.Collection;

    public class TreeNode {

        int data;

        Collection<TreeNode> children;

    }

    A generic tree node may contain any number of children. We can use any Collection to hold them.

    GRAPH NODE

    package com.javajee.cse.dsa;

    import java.util.Collection;

    public class GraphNode {

        int data;

        Collection<GraphNode>  adjacentNodes;

        boolean visited;

    }

    A graph node will have pointers to its adjacent nodes. We can use any collection to hold the adjacent nodes. We can also use a boolean variable that can be used to see if a node is already visited while traversing, as graph can have circular links.

    Recent Articles

    OAUTH – FREQUENTLY ASKED QUESTIONS FOR INTERVIEWS AND SELF EVALUATION

    Why is refresh token needed when you have access token? Access tokens are usually short-lived and refresh tokens are...

    SUMO LOGIC VIDEOS AND TUTORIALS

    Sumo Logic Basics - Part 1 of 2 (link is external) (Sep 29, 2016)Sumo Logic Basics - Part 2 of 2...

    GIT – USEFUL COMMANDS

    Discard all local changes, but save them for possible re-use later:  git stash Discarding local changes...

    DISTRIBUTED COMPUTING – RECORDED LECTURES (BITS)

    Module 1 - INTRODUCTION Recorded Lecture - 1.1 Introduction Part I – Definition

    BOOK REVIEW GUIDELINES FOR COOKBOOKS

    Whenever you add reviews for the book, please follow below rules. Write issues in an excel.Create an excel...

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox