Binary Tree Connected

Binary Tree Connected
1. Node.java

 package com.nrs.ds.tree.ex;  
 public class Node {  
      String data;  
      Node left, right, nextRight;  
      Node(String string){  
           data = string;  
           left=right=nextRight=null;  
      }  
 }  
2.BinaryTreeConnected.java

 package com.nrs.ds.tree.ex;  
 public class BinaryTreeConnected {  
      Node root;  
      void connect(Node p){  
           p.nextRight = null;  
           connectRecur(p);  
      }  
      void connectRecur(Node p){  
           if(p == null){  
                return;  
           }  
           if(p.left != null){  
                p.left.nextRight = p.right;  
           }  
           if(p.right != null){  
                p.right.nextRight = (p.nextRight != null) ? p.nextRight.left:null;  
           }  
           connectRecur(p.left);  
           connectRecur(p.right);  
      }  
 }  
3. Test
 package com.nrs.ds.tree.ex;  
 public class BinaryTreeConnectTest {  
      public static void main(String[] args) {  
           BinaryTreeConnected btc = new BinaryTreeConnected();  
           btc.root = new Node("A");  
           btc.root.left = new Node("B");  
           btc.root.right = new Node("C");  
           btc.root.left.left = new Node("D");  
           btc.root.left.right = new Node("E");  
           btc.connect(btc.root);  
            System.out.println("Following are populated nextRight pointers in the tree (-1 is printed if there is no neextRight)");  
            String a = btc.root.nextRight != null ? btc.root.nextRight.data : "NULL";  
            System.out.println("nextRight of "+ btc.root.data +"is "+a);  
            String b = btc.root.left.nextRight != null ? btc.root.left.nextRight.data : "NULL";  
            System.out.println("nextRight of "+ btc.root.left.data +"is "+b);  
            String c = btc.root.right.nextRight != null ? btc.root.right.nextRight.data :"NULL";  
            System.out.println("nextRight of "+ btc.root.right.data +"is "+c);  
            String d = btc.root.left.left.nextRight != null ? btc.root.left.left.nextRight.data : "NULL";  
            System.out.println("nextRight of "+ btc.root.left.left.data +"is "+d);  
      }  
 }  
Output:

Comments

Popular posts from this blog

Tournament Tree or Winner Tree

Linked List

Heaps