Skip to main content

How to design a Node in Tree?

How to design a Node in Tree?

There are three main components of a tree in a node.

 1) Integer holding data.
2) Left pointer holding node in a left subtree.
3) Right pointer holding node in a right subtree.

The following design is having data of int type and left,right pointers of a node to the subtrees.


package com.BST;

public class Node {

 int data;
 Node left;
 Node right;
 /**
  * @return the data
  */
 public Node(int data)
 {
  this.left=null;
  this.right=null;
  this.data=data;
 }
 
 public int getData() {
  return data;
 }
 /**
  * @param data the data to set
  */
 public void setData(int data) {
  this.data = data;
 }
 /**
  * @return the left
  */
 public Node getLeft() {
  return left;
 }
 /**
  * @param left the left to set
  */
 public void setLeft(Node left) {
  this.left = left;
 }
 /**
  * @return the right
  */
 public Node getRight() {
  return right;
 }
 /**
  * @param right the right to set
  */
 public void setRight(Node right) {
  this.right = right;
 }
 /* (non-Javadoc)
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
  return "Node [data=" + data + ", left=" + left + ", right=" + right
    + "]";
 }
 
 

}

Comments

.

Popular posts from this blog

5 steps to add existing project to your GIT HUB repository [Windows]

this tutorial will tell you how to add existing project to your git hub account. Step1. Go to your GITHUB account. Create New repository  add repository Step2. Fill the required details. ( uncheck create read me ). Add details   Step 3. Open GIT shell from your desktop. Step 4. Go to the directory of your project using cd  “path of the project” Initialize git by using command   git init Add your files by using command   git add . Commit your files by command git commit –m ‘first commit’ commands to add git   STEP 5 Select your repository Add using command git remote add origin https://github.com/username/projectname.git Now push the project to git git push -u origin master push command

Best LeetCode Lists for Interviews

Here is a list of some of the best questions asked in interviews Must do Top interview questions Top 100 liked Must do 75  Must do 60  Must do medium Data structures Tree Graph  Dynamic Programming Company Interviews FaceBook interviews Amazon Interviews Google Interviews Github master List