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

Adding Node at the End of Linked List

Adding Node at the End of Linked List:- The method takes two parameter one a head node of a linked list and other data too insert. public void addNodeEnd ( Node head , int data ) { if ( head == null ) { System . out . println ( "list is empty" ); return ; } Node node = new Node ( data ); Node temp = head ; while ( temp . next != null ) { temp = temp . next ; } temp . next = node ; } To Know the basic structure of the linked list click here

DEPTH FIRST SEARCH IN JAVA

DEPTH FIRST SEARCH IN JAVA public void dfs () { vertex [ 0 ]. visited = true ; displyVertex ( 0 ); stackobj . push ( 0 ); while (! stackobj . isEmpty ()) { int v = adjVertex (( Integer ) stackobj . peek ()); if ( v ==- 1 ) { stackobj . pop (); } else { vertex [ v ]. visited = true ; displyVertex ( v ); stackobj . push ( v ); } } }