Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1.  
  2. public class BNS {
  3.     private BN root = null;
  4.    
  5.     public boolean insert(int element) {
  6.         BN toInsert = new BN(element);
  7.         BN child = root;
  8.         BN parent = null;
  9.        
  10.         if(child == null) {
  11.             child = toInsert;
  12.         }
  13.        
  14.         while(child != null) {
  15.             parent = child;
  16.             if(parent.element > element) {
  17.                 child = child.left;
  18.             }
  19.             else if (parent.element < element) {
  20.                 child = child.right;
  21.             }
  22.         }
  23.        
  24.         if(parent.element > element) {
  25.             parent.left = toInsert;
  26.             return true;
  27.         }
  28.         else if(parent.element < element) {
  29.             parent.right = toInsert;
  30.             return true;
  31.         }
  32.         return false;
  33.     }
  34.    
  35.     public void printPath(int element) {
  36.         String path = "";
  37.         BN current = root;
  38.        
  39.         if(current.element == element) {
  40.             System.out.println(path += "root");
  41.             return;
  42.         }
  43.         else {
  44.             while(current != null) {
  45.                 if(current.element == element) {
  46.                     System.out.println(path);
  47.                     return;
  48.                 }
  49.                 if(current.element > element) {
  50.                     current = current.left;
  51.                     path += "l";
  52.                 }
  53.                 else if(current.element < element) {
  54.                     current = current.right;
  55.                     path += "r";
  56.                 }
  57.             }
  58.         }
  59.         System.out.println(path);
  60.     }
  61. }
  62.  
  63.  
  64.  
  65.  
  66. public class BN {
  67.     public int element;
  68.     public BN left, right;
  69.    
  70.     public BN(int element) {
  71.         this.element = element;
  72.         this.left = null;
  73.         this.right = null;
  74.     }
  75.    
  76.     public BN(int element, BN left, BN right) {
  77.         this.element = element;
  78.         this.left = left;
  79.         this.right = right;
  80.     }
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. public class BNSTest {
  88.     public static void main(String[] args) {
  89.         BNS tree = new BNS();
  90.        
  91.         tree.insert(10);
  92.         tree.insert(52);
  93.         tree.insert(89);
  94.         tree.insert(4);
  95.         tree.insert(35);
  96.         tree.insert(20);
  97.         tree.insert(85);
  98.         tree.insert(12);
  99.         tree.insert(1);
  100.         tree.insert(8);
  101.         tree.insert(7);
  102.         tree.insert(2);
  103.         tree.insert(3);
  104.        
  105.        
  106.         tree.printPath(12);
  107.        
  108.         System.out.println("Hallo");
  109.        
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement