Advertisement
Guest User

Untitled

a guest
May 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.80 KB | None | 0 0
  1. package nowe_algo_4;
  2.  
  3. class BinarySearchTree {
  4.     long comp=0;
  5.     long mod=0;
  6. static class Node {
  7.      String key;
  8.      Node left, right,parent;
  9.  
  10.      public Node(String item) {
  11.          key = item;
  12.          left = right = parent = null;
  13.      }
  14.      
  15.  }
  16. static Node newNode(String data)  
  17. {  
  18.     Node temp = new Node("");  
  19.    
  20.     temp.key = data;  
  21.    
  22.     temp.left = null;  
  23.     temp.right = null;  
  24.    
  25.     return temp;  
  26. }  
  27. static Node root;
  28.  BinarySearchTree() {
  29.    root = null;
  30.  }
  31.   Node insert2(String key)  
  32.  {  
  33.      Node newnode = newNode(key);  
  34.      
  35.      Node x = root;  
  36.      
  37.      Node y = null;  
  38.      
  39.      while (root != null) {  
  40.          comp++;
  41.          y = root;  
  42.          comp++;
  43.          if (key.compareTo(root.key) < 0) {
  44.              root = root.left;  
  45.              mod++;
  46.          }
  47.          else{
  48.              root = root.right;
  49.              mod++;
  50.          }
  51.      }  
  52.      comp++;
  53.      if (root == null) {
  54.          root = newnode;
  55.          mod++;
  56.      }
  57.    
  58.      else if (key.compareTo(root.key) < 0) {
  59.          comp++;
  60.          root.left = newnode;  
  61.      }
  62.      
  63.    
  64.      else{
  65.          comp++;
  66.          mod++;
  67.          root.right = newnode;
  68.      }
  69.      
  70.      return root;  
  71.  }  
  72.  void insert(String key) {
  73.      char insert = key.charAt(key.length() - 1);
  74.      if (!((insert >= 'a' && insert <= 'z') || (insert >= 'A' && insert <= 'Z'))) {
  75.          key = key.substring(0, key.length() - 1);
  76.      }
  77.      insert = key.charAt(0);
  78.      if (!((insert >= 'a' && insert <= 'z') || (insert >= 'A' && insert <= 'Z'))) {
  79.          key = key.substring(1);
  80.      }
  81.     root = insertRec(root, key);
  82.  }
  83.  Node insertRec(Node root, String key) {
  84.      comp++;
  85.      if (root == null) {
  86.          mod++;
  87.          root = new Node(key);
  88.          return root;
  89.      }
  90.      else{
  91.  
  92.          Node temp=null;
  93.          comp++;
  94.      if (key.compareTo(root.key)<0){
  95.          temp = insertRec(root.left, key);
  96.          mod++;;
  97.          root.left=temp;
  98.          temp.parent=root;
  99.      }
  100.      else if (key.compareTo(root.key)>0){
  101.          comp++;
  102.          temp = insertRec(root.right, key);
  103.          mod++;
  104.          root.right=temp;
  105.          temp.parent=root;
  106.      }
  107.      return root;
  108.      }    
  109.  }
  110.  
  111.  void inorder()  {
  112.     inorderRec(root);
  113.  }
  114.  void inorderRec(Node root) {
  115.      if (root != null) {
  116.          inorderRec(root.left);
  117.          System.out.println(root.key);
  118.          inorderRec(root.right);
  119.      }
  120.  }
  121.  void deleteKey(String key)
  122.  {
  123.      root = deleteRec(root, key);
  124.  }
  125.  Node deleteRec(Node root, String key)
  126.  {
  127.    
  128.      comp++;
  129.      if (root == null){
  130.          mod++;
  131.          return root;
  132.      
  133.      }
  134.      if (key.compareTo(root.key)<0){
  135.          comp++;
  136.          //mod++;
  137.          root.left = deleteRec(root.left, key);
  138.      }
  139.      else if (key.compareTo(root.key)>0){
  140.          comp+=2;
  141.         // mod++;
  142.          root.right = deleteRec(root.right, key);
  143.      }
  144.      else
  145.      {
  146.          comp++;
  147.          if (root.left == null){
  148.              mod++;
  149.              return root.right;
  150.              
  151.          }
  152.          else if (root.right == null){
  153.              comp++;
  154.              mod++;
  155.              return root.left;
  156.          }
  157.          mod+=2;
  158.          root = min(root.right);
  159.          root.right = deleteRec(root.right, root.key);
  160.      }
  161.  
  162.      return root;
  163.  }
  164.  
  165.  Node min(Node root)
  166.  {
  167.      if(root==null){
  168.          return root;
  169.      }
  170.      Node minv = root;
  171.      while (root.left != null)
  172.      {
  173.          minv = root.left;
  174.          root = root.left;
  175.      }
  176.      return minv;
  177.  }
  178.  Node max(Node root)
  179.  {
  180.      if(root==null){
  181.          return root;
  182.      }
  183.      Node max = root;
  184.      while (root.right != null)
  185.      {
  186.          max = root.right;
  187.          root = root.right;
  188.      }
  189.      return max;
  190.  }
  191.  public Node search(Node root, String key)
  192.  {
  193.      comp++;
  194.      if(root==null||root.key.equals(key))
  195.          return root;
  196.      comp++;
  197.      if (key.compareTo(root.key)<0){
  198.          return search(root.left, key);
  199.      }
  200.    
  201.      return search(root.right, key);
  202.  }
  203.  
  204.  public int find(String key){
  205.      comp++;
  206.      if(search(root,key)==null)
  207.          return 0;
  208.      else
  209.          return 1;
  210.  }
  211.  void successor(String value) {
  212.      Node temporary = new Node(value);
  213.      Node valueNode = successorVal(root, temporary);
  214.      if (valueNode != null) {
  215.          System.out.println(valueNode.key);
  216.      } else {
  217.          System.out.println("");
  218.      }
  219.  }
  220.  Node successorVal(Node root, Node value) {
  221.      Node successor = null;
  222.      if (value.right != null) {
  223.          return min(value.right);
  224.      }
  225.      while (root != null) {
  226.          if(value.key.compareTo(root.key) < 0) {
  227.              successor = root;
  228.              root = root.left;
  229.          } else if(value.key.compareTo(root.key) > 0) {
  230.              root = root.right;
  231.          } else {
  232.              break;
  233.          }
  234.            
  235.      }
  236.      return successor;
  237.  }
  238. }
  239. package nowe_algo_4;
  240.  
  241. public class RedBlackTree {
  242.  
  243.     int RED = 0;
  244.     int BLACK = 1;
  245.     int comp=0;
  246.     int mod=0;
  247.  
  248.      class Node {
  249.         String key;
  250.         int color = BLACK;
  251.         Node left = nil, right = nil, parent = nil;
  252.  
  253.         public Node(String key) {
  254.             this.key = key;
  255.         }
  256.     }
  257.  
  258.     Node nil = new Node(null);
  259.     Node root=nil ;
  260.     RedBlackTree() {
  261.         root = nil;
  262.     }
  263.    
  264.     public Node search(Node root, String key)
  265.     {
  266.         comp++;
  267.         if(root==nil||root.key.equals(key))
  268.             return root;
  269.         comp++;
  270.         if (key.compareTo(root.key)<0){
  271.             return search(root.left, key);
  272.         }
  273.        
  274.         return search(root.right, key);
  275.     }
  276.    
  277.     public int find(String key){
  278.         comp++;
  279.         if(search(root,key)==nil)
  280.             return 0;
  281.         else
  282.             return 1;
  283.     }
  284.     void insert(String value) {
  285.         Node node = new Node(value);
  286.         char insert = value.charAt(value.length() - 1);
  287.         if (!((insert >= 'a' && insert <= 'z') || (insert >= 'A' && insert <= 'Z'))) {
  288.             value = value.substring(0, value.length() - 1);
  289.         }
  290.         insert = value.charAt(0);
  291.         if (!((insert >= 'a' && insert <= 'z') || (insert >= 'A' && insert <= 'Z'))) {
  292.             value = value.substring(1);
  293.         }
  294.         Node temporary = root;
  295.         comp++;
  296.         if (root == nil) {
  297.             mod++;
  298.             root = node;
  299.             node.color = BLACK;
  300.             node.parent = nil;
  301.         } else {
  302.             node.color = RED;
  303.             while (true) {
  304.                 comp++;
  305.                 if (node.key.compareTo(temporary.key) < 0) {
  306.                     comp++;
  307.                     if (temporary.left == nil) {
  308.                         mod++;
  309.                         temporary.left = node;
  310.                         node.parent = temporary;
  311.                         break;
  312.                     } else {
  313.                         temporary = temporary.left;
  314.                     }
  315.                 } else if (node.key.compareTo(temporary.key) >= 0) {
  316.                     comp+=2;
  317.                     if (temporary.right == nil) {
  318.                         mod++;
  319.                         temporary.right = node;
  320.                         node.parent = temporary;
  321.                         break;
  322.                     } else {
  323.                         temporary = temporary.right;
  324.                     }
  325.                 }
  326.             }
  327.             fixTree(node);
  328.         }
  329.     }
  330.     private void fixTree(Node node) {
  331.         while (node.parent.color == RED) {
  332.             comp++;
  333.             Node uncle = nil;
  334.             comp++;
  335.             if (node.parent == node.parent.parent.left) {
  336.                 uncle = node.parent.parent.right;
  337.          
  338.                 comp++;
  339.                 if (uncle != nil && uncle.color == RED) {
  340.                     //mod++;
  341.                     node.parent.color = BLACK;
  342.                     uncle.color = BLACK;
  343.                     node.parent.parent.color = RED;
  344.                     node = node.parent.parent;
  345.                     continue;
  346.                 }
  347.                 comp++;
  348.                 if (node == node.parent.right) {
  349.                     //Double rotation needed
  350.                     mod++;
  351.                     node = node.parent;
  352.                     rotateLeft(node);
  353.                 }
  354.                
  355.                 node.parent.color = BLACK;
  356.                 node.parent.parent.color = RED;
  357.                 //if the "else if" code hasn't executed, this
  358.                 //is a case where we only need a single rotation
  359.                 rotateRight(node.parent.parent);
  360.             } else {
  361.                 uncle = node.parent.parent.left;
  362.                 comp++;
  363.                  if (uncle != nil && uncle.color == RED) {
  364.                     // mod++;
  365.                     node.parent.color = BLACK;
  366.                     uncle.color = BLACK;
  367.                     node.parent.parent.color = RED;
  368.                     node = node.parent.parent;
  369.                     continue;
  370.                 }
  371.                  comp++;
  372.                 if (node == node.parent.left) {
  373.                     //Double rotation needed
  374.                     node = node.parent;
  375.                     rotateRight(node);
  376.                 }
  377.                 node.parent.color = BLACK;
  378.                 node.parent.parent.color = RED;
  379.                 //if the "else if" code hasn't executed, this
  380.                 //is a case where we only need a single rotation
  381.                 rotateLeft(node.parent.parent);
  382.             }
  383.         }
  384.         root.color = BLACK;
  385.     }
  386.  
  387.     void rotateLeft(Node node) {
  388.         comp++;
  389.         if (node.parent != nil) {
  390.             comp++;
  391.             if (node == node.parent.left) {
  392.                 mod++;
  393.                 node.parent.left = node.right;
  394.             } else {
  395.                 mod++;
  396.                 node.parent.right = node.right;
  397.             }
  398.             mod+=1;
  399.             node.right.parent = node.parent;
  400.             node.parent = node.right;
  401.             comp++;
  402.             if (node.right.left != nil) {
  403.                 mod++;
  404.                 node.right.left.parent = node;
  405.             }
  406.             mod+=1;
  407.             node.right = node.right.left;
  408.             node.parent.left = node;
  409.         } else {//Need to rotate root
  410.             Node right = root.right;
  411.             root.right = right.left;
  412.             right.left.parent = root;
  413.             root.parent = right;
  414.             right.left = root;
  415.             right.parent = nil;
  416.             root = right;
  417.             mod+=6;
  418.         }
  419.     }
  420.  
  421.     void rotateRight(Node node) {
  422.         comp++;
  423.         if (node.parent != nil) {
  424.             comp++;
  425.             if (node == node.parent.left) {
  426.                 node.parent.left = node.left;
  427.             } else {
  428.                 mod++;
  429.                 node.parent.right = node.left;
  430.             }
  431.             mod+=2;
  432.             node.left.parent = node.parent;
  433.             node.parent = node.left;
  434.             comp++;
  435.             if (node.left.right != nil) {
  436.                 mod++;
  437.                 node.left.right.parent = node;
  438.             }
  439.             mod+=2;
  440.             node.left = node.left.right;
  441.             node.parent.right = node;
  442.         } else {//Need to rotate root
  443.             Node left = root.left;
  444.             root.left = root.left.right;
  445.             left.right.parent = root;
  446.             root.parent = left;
  447.             left.right = root;
  448.             left.parent = nil;
  449.             root = left;
  450.             mod+=6;
  451.         }
  452.     }
  453.     void transplant(Node target, Node with){
  454.         comp++;
  455.           if(target.parent == nil){
  456.               root = with;
  457.               mod++;
  458.           }else if(target == target.parent.left){
  459.               comp++;
  460.               mod++;
  461.               target.parent.left = with;
  462.           }else{
  463.               comp++;
  464.               mod++;
  465.               target.parent.right = with;
  466.           }
  467.          
  468.           with.parent = target.parent;
  469.     }
  470.    
  471.     boolean delete(String key){
  472.         Node z;
  473.         comp++;
  474.         if((z = search(root,key))==nil) return false;
  475.         Node x;
  476.         Node y = z; // temporary reference y
  477.         int y_original_color = y.color;
  478.         comp++;
  479.         if(z.left == nil){
  480.             x = z.right;  
  481.             transplant(z, z.right);  
  482.         }else if(z.right == nil){
  483.             comp++;
  484.             x = z.left;
  485.             transplant(z, z.left);
  486.         }else{
  487.             comp++;
  488.             y = min(z.right);
  489.             y_original_color = y.color;
  490.             x = y.right;
  491.             comp++;
  492.             if(y.parent == z){
  493.                 x.parent = y;
  494.               //  mod++;
  495.             }
  496.             else{
  497.                 transplant(y, y.right);
  498.                // mod+=2;
  499.                 y.right = z.right;
  500.                 y.right.parent = y;
  501.             }
  502.             transplant(z, y);
  503.           //  mod+=2;
  504.             y.left = z.left;
  505.             y.left.parent = y;
  506.             y.color = z.color;
  507.         }
  508.         comp++;
  509.         if(y_original_color==BLACK)
  510.             deleteFixup(x);  
  511.         return true;
  512.     }
  513.    
  514.     void deleteFixup(Node x){
  515.         while(x!=root && x.color == BLACK){
  516.             comp++;
  517.             if(x == x.parent.left){
  518.                 Node w = x.parent.right;
  519.                 comp++;
  520.                 if(w.color == RED){
  521.                     //mod++;
  522.                     w.color = BLACK;
  523.                     x.parent.color = RED;
  524.                     rotateLeft(x.parent);
  525.                     w = x.parent.right;
  526.                 }
  527.                 comp++;
  528.                 if(w.left.color == BLACK && w.right.color == BLACK){
  529.                     //mod++;
  530.                     w.color = RED;
  531.                     x = x.parent;
  532.                     continue;
  533.                 }
  534.                 else if(w.right.color == BLACK){
  535.                 //  mod++;
  536.                     comp++;
  537.                     w.left.color = BLACK;
  538.                     w.color = RED;
  539.                     rotateRight(w);
  540.                     w = x.parent.right;
  541.                 }
  542.                 comp++;
  543.                 if(w.right.color == RED){
  544.                     //mod++;
  545.                     w.color = x.parent.color;
  546.                     x.parent.color = BLACK;
  547.                     w.right.color = BLACK;
  548.                     rotateLeft(x.parent);
  549.                     x = root;
  550.                 }
  551.             }else{
  552.                 Node w = x.parent.left;
  553.                 if(w.color == RED){
  554.                     //mod++;
  555.                     w.color = BLACK;
  556.                     x.parent.color = RED;
  557.                     rotateRight(x.parent);
  558.                     w = x.parent.left;
  559.                 }
  560.                 comp++;
  561.                 if(w.right.color == BLACK && w.left.color == BLACK){
  562.                     w.color = RED;
  563.                     x = x.parent;
  564.                     continue;
  565.                 }
  566.                 else if(w.left.color == BLACK){
  567.                     //mod++;
  568.                     comp++;
  569.                     w.right.color = BLACK;
  570.                     w.color = RED;
  571.                     rotateLeft(w);
  572.                     w = x.parent.left;
  573.                 }
  574.                 comp++;
  575.                 if(w.left.color == RED){
  576.                     //mod++;
  577.                     w.color = x.parent.color;
  578.                     x.parent.color = BLACK;
  579.                     w.left.color = BLACK;
  580.                     rotateRight(x.parent);
  581.                     x = root;
  582.                 }
  583.             }
  584.         }
  585.         x.color = BLACK;
  586.     }
  587.    
  588.     Node min(Node root)
  589.     {
  590.      if(root==nil){
  591.          return root;
  592.      }
  593.         Node minv = root;
  594.         while (root.left != nil)
  595.         {
  596.             minv = root.left;
  597.             root = root.left;
  598.         }
  599.         return minv;
  600.     }
  601.     Node max(Node root)
  602.     {
  603.      if(root==nil){
  604.          return root;
  605.      }
  606.         Node max = root;
  607.         while (root.right != nil)
  608.         {
  609.             max = root.right;
  610.             root = root.right;
  611.         }
  612.         return max;
  613.     }
  614.        
  615.    
  616.     void inorder()  {
  617.         inorderRec(root);
  618.      }
  619.      void inorderRec(Node root) {
  620.          if (root != null) {
  621.              inorderRec(root.left);
  622.              if(root.key!=null)
  623.              System.out.println(root.key);
  624.              inorderRec(root.right);
  625.          }
  626.      }
  627.      void successor(String value) {
  628.          Node temporary = new Node(value);
  629.          Node valueNode = successorVal(root, temporary);
  630.          if (valueNode != null) {
  631.              System.out.println(valueNode.key);
  632.          } else {
  633.              System.out.println("");
  634.          }
  635.      }
  636.      Node successorVal(Node root, Node value) {
  637.          Node successor = null;
  638.          if (value.right != null) {
  639.              return min(value.right);
  640.          }
  641.          while (root != null) {
  642.              if(value.key.compareTo(root.key) < 0) {
  643.                  
  644.                  successor = root;
  645.                  root = root.left;
  646.              } else if(value.key.compareTo(root.key) > 0) {
  647.                
  648.                  root = root.right;
  649.              } else {
  650.                  break;
  651.              }
  652.                
  653.          }
  654.          return successor;
  655.      }
  656.      
  657. }
  658. package nowe_algo_4;
  659.  
  660.  class SplayNode
  661.  {    
  662.      SplayNode left, right, parent;
  663.      String element;
  664.      public SplayNode()
  665.      {
  666.          this("", null, null, null);
  667.      }          
  668.      public SplayNode(String ele)
  669.      {
  670.          this(ele, null, null, null);
  671.      }
  672.      public SplayNode(String ele, SplayNode left, SplayNode right, SplayNode parent)
  673.      {
  674.          this.left = left;
  675.          this.right = right;
  676.          this.parent = parent;
  677.          this.element = ele;        
  678.      }    
  679.  }
  680.  class SplayTree
  681.  {
  682.      int comp=0;
  683.      int mod=0;
  684.      private SplayNode root;
  685.      private int count = 0;
  686.      public SplayTree()
  687.      {
  688.          root = null;
  689.      }
  690.      public boolean isEmpty()
  691.      {
  692.          return root == null;
  693.      }
  694.      public void clear()
  695.      {
  696.          root = null;
  697.          count = 0;
  698.      }
  699.      public void insert(String ele)
  700.      {
  701.          SplayNode z = root;
  702.          SplayNode p = null;
  703.          while (z != null)
  704.          {comp++;
  705.              p = z;
  706.              comp++;
  707.              if (ele.compareTo(p.element) > 0)
  708.                  z = z.right;
  709.              else
  710.                  z = z.left;
  711.          }
  712.          z = new SplayNode();
  713.          z.element = ele;
  714.          z.parent = p;
  715.          comp++;
  716.          if (p == null)
  717.              root = z;
  718.          else if (ele.compareTo(p.element) > 0){
  719.              p.right = z;
  720.              comp++;
  721.          }
  722.          else{
  723.              comp++;
  724.              p.left = z;
  725.          }
  726.          Splay(z);
  727.          count++;
  728.      }
  729.      public void makeLeftChildParent(SplayNode c, SplayNode p)
  730.      {
  731.          comp++;
  732.          if ((c == null) || (p == null) || (p.left != c) || (c.parent != p))
  733.              throw new RuntimeException("WRONG");
  734.          comp++;
  735.          if (p.parent != null)
  736.          {
  737.              comp++;
  738.              if (p == p.parent.left){
  739.                  p.parent.left = c;
  740.                  mod++;
  741.              }
  742.              else {
  743.                  mod++;
  744.                  p.parent.right = c;
  745.              }
  746.          }
  747.          comp++;
  748.          if (c.right != null){
  749.              c.right.parent = p;
  750.              mod++;
  751.          }
  752.          mod+=4;
  753.          c.parent = p.parent;
  754.          p.parent = c;
  755.          p.left = c.right;
  756.          c.right = p;
  757.      }
  758.      public void makeRightChildParent(SplayNode c, SplayNode p)
  759.      {
  760.          comp++;
  761.          if ((c == null) || (p == null) || (p.right != c) || (c.parent != p))
  762.              throw new RuntimeException("WRONG");
  763.          comp++;
  764.          if (p.parent != null)
  765.          {
  766.              comp++;
  767.              if (p == p.parent.left){
  768.                  p.parent.left = c;
  769.                  mod++;
  770.              }
  771.              else{
  772.                  p.parent.right = c;
  773.                  mod++;
  774.              }
  775.          }
  776.          comp++;
  777.          if (c.left != null){
  778.              mod++;
  779.              c.left.parent = p;
  780.          }
  781.          mod+=4;
  782.          c.parent = p.parent;
  783.          p.parent = c;
  784.          p.right = c.left;
  785.          c.left = p;
  786.      }
  787.      private void Splay(SplayNode x)
  788.      {
  789.          while (x.parent != null)
  790.          {
  791.              comp++;
  792.              SplayNode Parent = x.parent;
  793.              SplayNode GrandParent = Parent.parent;
  794.              comp++;
  795.              if (GrandParent == null)
  796.              {
  797.                  comp++;
  798.                  if (x == Parent.left)
  799.                      makeLeftChildParent(x, Parent);
  800.                  else
  801.                      makeRightChildParent(x, Parent);                
  802.              }
  803.              else
  804.              {
  805.                  comp++;
  806.                  if (x == Parent.left)
  807.                  {
  808.                      comp++;
  809.                      if (Parent == GrandParent.left)
  810.                      {
  811.                          makeLeftChildParent(Parent, GrandParent);
  812.                          makeLeftChildParent(x, Parent);
  813.                      }
  814.                      else
  815.                      {
  816.                          makeLeftChildParent(x, x.parent);
  817.                          makeRightChildParent(x, x.parent);
  818.                      }
  819.                  }
  820.                  else
  821.                  {
  822.                      comp++;
  823.                      if (Parent == GrandParent.left)
  824.                      {
  825.                          makeRightChildParent(x, x.parent);
  826.                          makeLeftChildParent(x, x.parent);
  827.                      }
  828.                      else
  829.                      {
  830.                          makeRightChildParent(Parent, GrandParent);
  831.                          makeRightChildParent(x, Parent);
  832.                      }
  833.                  }
  834.              }
  835.          }
  836.          root = x;
  837.      }
  838.      public void remove(String ele)
  839.      {
  840.          SplayNode node = findNode(ele);
  841.         remove(node);
  842.      }
  843.      private void remove(SplayNode node)
  844.      {
  845.          comp++;
  846.          if (node == null)
  847.              return;
  848.          Splay(node);
  849.          comp++;
  850.          if( (node.left != null) && (node.right !=null))
  851.          {
  852.              SplayNode min = node.left;
  853.              while(min.right!=null){
  854.                  min = min.right;
  855.                  comp++;
  856.              }
  857.              min.right = node.right;
  858.              node.right.parent = min;
  859.              node.left.parent = null;
  860.              root = node.left;
  861.              mod+=3;
  862.          }
  863.          else if (node.right != null)
  864.          {
  865.              comp++;
  866.              mod+=2;
  867.              node.right.parent = null;
  868.              root = node.right;
  869.          }
  870.          else if( node.left !=null)
  871.          {
  872.              comp+=2;
  873.              mod+=2;
  874.              node.left.parent = null;
  875.              root = node.left;
  876.          }
  877.          else
  878.          {
  879.              mod+=2;
  880.              root = null;
  881.          }
  882.          mod+=4;
  883.          node.parent = null;
  884.          node.left = null;
  885.          node.right = null;
  886.          node = null;
  887.          count--;
  888.      }
  889.      public int countNodes()
  890.      {
  891.          return count;
  892.      }
  893.      public int search(String val)
  894.      {
  895.          comp++;
  896.          if(findNode(val) != null)
  897.              return 1;
  898.          return 0;
  899.      }
  900.      private SplayNode findNode(String ele)
  901.      {
  902.          SplayNode PrevNode = null;
  903.          SplayNode z = root;
  904.          while (z != null)
  905.          {
  906.              comp++;
  907.              PrevNode = z;
  908.              comp++;
  909.              if (ele.compareTo(z.element) > 0)
  910.                  z = z.right;
  911.              else if (ele.compareTo(z.element) < 0){
  912.                  z = z.left;
  913.                  comp++;
  914.              }
  915.              else if(ele.compareTo(z.element)==0) {
  916.                  comp+=2;
  917.                  Splay(z);
  918.                  return z;
  919.              }
  920.          }
  921.          comp++;
  922.          if(PrevNode != null)
  923.          {
  924.              Splay(PrevNode);
  925.              return null;
  926.          }
  927.          return null;
  928.      }
  929.      public void inorder()
  930.      {
  931.          inorder(root);
  932.      }
  933.      private void inorder(SplayNode r)
  934.      {
  935.          if (r != null)
  936.          {
  937.              inorder(r.left);
  938.              System.out.print(r.element +" ");
  939.              inorder(r.right);
  940.          }
  941.      }
  942.      public void preorder()
  943.      {
  944.          preorder(root);
  945.      }
  946.      private void preorder(SplayNode r)
  947.      {
  948.          if (r != null)
  949.          {
  950.              System.out.print(r.element +" ");
  951.              preorder(r.left);            
  952.              preorder(r.right);
  953.          }
  954.      }
  955.      public void postorder()
  956.      {
  957.          postorder(root);
  958.      }
  959.      private void postorder(SplayNode r)
  960.      {
  961.          if (r != null)
  962.          {
  963.              postorder(r.left);            
  964.              postorder(r.right);
  965.              System.out.print(r.element +" ");
  966.          }
  967.      }    
  968.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement