Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. class PatriciaTree<T> {
  2.     class Node<T> {
  3.         public Node(String key, int bitPos, Node succ) {
  4.             m_Key = key;
  5.             m_BitPos = bitPos;
  6.             boolean blsRight = (key.charAt(0) & (1 << bitPos)) != 0;
  7.             m_Left = new Node(blsRight ? succ : this);
  8.             m_Right = new Node(blsRight ? this : succ);
  9.         }
  10.         public Node(String key, int bitPos) {
  11.             this(key,bitPos,null);
  12.         }
  13.         public Node(Node succ) {
  14.             this("",0,succ);
  15.         }
  16.         public int getNrOfMatchingChars(String key) {
  17.             int nrOfMatchingChars = 0;
  18.             while (nrOfMatchingChars < m_Key.length() && nrOfMatchingChars < this.m_Key.length()) {
  19.                 if (m_Key.charAt(nrOfMatchingChars) != this.m_Key.charAt(nrOfMatchingChars)) {
  20.                   break;
  21.                 }
  22.                 nrOfMatchingChars++;
  23.             }
  24.             return nrOfMatchingChars;
  25.         }
  26.        
  27.         public String m_Key;
  28.         public int m_BitPos;
  29.         public Node m_Left;
  30.         public Node m_Right;       
  31.     }
  32.    
  33.     public boolean search(String s) {
  34.         Node node = m_Root;
  35.         int lastBitPos = -1;
  36.         String newS = "";
  37.         while (node != null && lastBitPos < node.m_BitPos) {
  38.             lastBitPos = node.m_BitPos;
  39.             int i = node.getNrOfMatchingChars(s);
  40.             node = (newS.charAt(i+1) & (1 << node.m_BitPos)) != 0 ? node.m_Right : node.m_Left;
  41.         }
  42.         return node != null && node.m_Key == s;
  43.     }
  44.    
  45.     public void insert(String key, T value) {
  46.         Node node = m_Root;
  47.         int lastBitPos = -1;
  48.         String newStr = "";
  49.         while (node != null && lastBitPos < node.m_BitPos) {
  50.             lastBitPos = node.m_BitPos;
  51.             int i = node.getNrOfMatchingChars(key);
  52.             newStr = key.substring(i,node.m_Key.length());
  53.             node = (newStr.charAt(0) & (1 << node.m_BitPos)) != 0 ? node.m_Right : node.m_Left;
  54.         }
  55.         if (node == null) {
  56.             node = new Node(key,lastBitPos+1);
  57.         } else if (newStr.charAt(0) != node.m_Key.charAt(node.getNrOfMatchingChars(key)+1)) {
  58.             int i = 0;
  59.             while
  60.         }
  61.     }
  62.    
  63.     private Node m_Root = new Node("",-1,null);
  64. }
  65.  
  66. public class u11a1 {
  67.     public static void main(String[] args) {
  68.         PatriciaTree pt = new PatriciaTree();
  69.     }
  70. }
Add Comment
Please, Sign In to add comment