seberm

binary_tree

Nov 10th, 2010
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1.  
  2. package binarytree;
  3.  
  4. /**
  5.  * @author Otto Sabart - Seberm
  6.  * 11/10/2010
  7.  */
  8. public class Node {
  9.     // Artibutes: //
  10.     private int m_key;
  11.     private Node m_left = null;
  12.     private Node m_right = null;
  13.  
  14.  
  15.     // Constructors: //
  16.     public Node() {
  17.  
  18.         m_key = 0;
  19.     }
  20.  
  21.    
  22.     public Node(int key) {
  23.  
  24.         m_key = key;
  25.     }
  26.  
  27.  
  28.     // Methods: //
  29.     public int getKey() {
  30.  
  31.         return m_key;
  32.     }
  33.  
  34.  
  35.     public void insertKey(int key) {
  36.  
  37.         if (key > m_key) {
  38.  
  39.             if (m_right == null)
  40.                 m_right = new Node(key);
  41.             else m_right.insertKey(key);
  42.  
  43.         } else if (m_left == null)
  44.             m_left = new Node(key);
  45.         else m_left.insertKey(key);
  46.     }
  47.  
  48.  
  49.     public void removeKey() {
  50.  
  51.         /*
  52.          * @todo dodelat odstaneni klice...tak aby nezustavala zapolnena pamet..a pointer se mezi nodama posouval spravne
  53.          */
  54.     }
  55.  
  56.  
  57.     public int getMax() {
  58.  
  59.         if (m_right == null)
  60.             return m_key;
  61.         else return m_right.getMax();
  62.     }
  63.  
  64.  
  65.     public int getMin() {
  66.  
  67.         if (m_left == null)
  68.             return m_key;
  69.         else return m_left.getMin();
  70.     }
  71.    
  72.  
  73.     public boolean searchKey(int key) {
  74.  
  75.         if (key == m_key)
  76.             return true;
  77.  
  78.         if (key > m_key) {
  79.  
  80.             if (m_right != null)
  81.                 return m_right.searchKey(key);
  82.             else return false;
  83.            
  84.         } else {
  85.  
  86.             if (m_left != null)
  87.                 return m_left.searchKey(key);
  88.             else return false;
  89.         }
  90.  
  91.    
  92.     }
  93.  
  94.  
  95.     public void printFindResult(int key) {
  96.  
  97.         if (searchKey(key))
  98.             System.out.printf("Program found number %d\n", key);
  99.         else System.out.printf("Program didn't find the number %d\n", key);
  100.     }
  101. }
Add Comment
Please, Sign In to add comment