Haytham_Shalabi

AVL&&BST Graph

Jan 12th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 KB | None | 0 0
  1.  
  2. /**
  3. *
  4. * @author haytham
  5. */
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9. /**
  10. *
  11. * @author Nasr
  12. */
  13. public class MyHomeWorkGraph {
  14. public static void main(String[] args){
  15. JFrame testHomeWork=new JFrame();
  16. testHomeWork.setSize(1000,500);
  17. testHomeWork.setResizable(false);
  18. testHomeWork.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. testHomeWork.add(new BinaryTreePanelShow());
  20. testHomeWork.setLocationRelativeTo(null);
  21. testHomeWork.setVisible(true);
  22.  
  23. }
  24.  
  25.  
  26.  
  27. }
  28.  
  29.  
  30. class BinaryTreePanelShow extends JPanel {
  31.  
  32. private JTextField InputText = new JTextField(10);
  33. private MyGraphPaint paintTree = new MyGraphPaint();
  34. private JButton Add = new JButton("Add");
  35. private JButton Delete = new JButton("Delete");
  36. private MyBST<Integer> myBST;
  37. public BinaryTreePanelShow() {
  38. this.myBST = new MyBST();
  39. BuildGUIAndActionListner();
  40.  
  41. }
  42.  
  43. private void BuildGUIAndActionListner() {
  44. InputText.setFont(new Font("Tahoma", 4, 10));
  45. this.setLayout(new BorderLayout());
  46. /* I have two panel the first One to Draw Binary tree and the seconde to put add
  47. and delete buttonand Test Input :D ^_^ ;
  48. */
  49. add(paintTree, BorderLayout.CENTER);
  50. JPanel P1 = new JPanel();
  51. P1.add(new JLabel("Enter The Element :"));
  52. P1.add(InputText);
  53. P1.add(Add);
  54. P1.add(Delete);
  55. add(P1, BorderLayout.SOUTH);
  56. P1.setBackground(Color.GRAY);
  57. Add.addActionListener(new ActionListener() {
  58. public void actionPerformed(ActionEvent e) {
  59. int element = Integer.parseInt(InputText.getText());//take the data from text
  60. //When We add the element first we should search for it
  61. // if we found the element we will not complete
  62. if (myBST.search(element)) {
  63. JOptionPane.showMessageDialog(null,element+" This element is already there..!");
  64. }
  65. else {
  66. myBST.add(element);
  67. paintTree.repaint();
  68. }
  69. }
  70. });
  71.  
  72. Delete.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74. int element = Integer.parseInt(InputText.getText());
  75. if (!myBST.search(element)) {
  76. JOptionPane.showMessageDialog(null, element + " is not in the There..!");
  77. }
  78. else {
  79. myBST.delete(element);
  80. paintTree.repaint();
  81. }
  82. }
  83. });
  84. }
  85.  
  86. class MyGraphPaint extends JPanel {
  87.  
  88. @Override
  89. public void setBackground(Color bg) {
  90. super.setBackground(Color.white);
  91. }
  92.  
  93. private int radius = 20;
  94. private int vericalDistance = 50;
  95.  
  96. protected void paintComponent(Graphics g) {
  97. super.paintComponent(g);
  98. if (myBST.root != null) {
  99. printTree(g,myBST.root,getWidth()/2,25,getWidth()/4);
  100. }
  101. }
  102.  
  103. private void printTree(Graphics g,Node root,int x, int y, int HorizantalDistance) {
  104. g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  105. g.drawString(root.data + "", x - 6, y + 4);
  106.  
  107. if (root.left != null) {
  108. connectLeftChild(g, x - HorizantalDistance, y + vericalDistance, x, y);
  109. printTree(g, root.left, x - HorizantalDistance, y + vericalDistance, HorizantalDistance / 3);
  110. }
  111.  
  112. if (root.right != null) {
  113. connectRightChild(g, x + HorizantalDistance, y + vericalDistance, x, y);
  114. printTree(g, root.right, x + HorizantalDistance, y + vericalDistance, HorizantalDistance / 3);
  115. }
  116. }
  117. private void connectLeftChild(Graphics g,int x1, int y1, int x2, int y2) {
  118. double d = Math.sqrt(vericalDistance * vericalDistance + (x2 - x1) * (x2 - x1));
  119. int x11 = (int)(x1 + radius * (x2 - x1) / d);
  120. int y11 = (int)(y1 - radius * vericalDistance / d);
  121. int x21 = (int)(x2 - radius * (x2 - x1) / d);
  122. int y21 = (int)(y2 + radius * vericalDistance / d);
  123. g.drawLine(x11, y11, x21, y21);
  124. }
  125.  
  126.  
  127. private void connectRightChild(Graphics g,int x1, int y1, int x2, int y2) {
  128. double d = Math.sqrt(vericalDistance * vericalDistance + (x2 - x1) * (x2 - x1));
  129. int x11 = (int)(x1 - radius * (x1 - x2) / d);
  130. int y11 = (int)(y1 - radius * vericalDistance / d);
  131. int x21 = (int)(x2 + radius * (x1 - x2) / d);
  132. int y21 = (int)(y2 + radius * vericalDistance / d);
  133. g.drawLine(x11, y11, x21, y21);
  134. }
  135. }
  136. }
  137. .................................................................................................ز
  138. import java.util.ArrayList;
  139. import java.util.Comparator;
  140. import java.util.Stack;
  141.  
  142. /**
  143. *
  144. * @author Nasr
  145. */
  146. public class MyBST<E> {
  147. public Node<E> root=null;
  148.  
  149.  
  150. public MyBST(){}
  151.  
  152. public MyBST(Comparator<E> comp)
  153. {
  154. root = null;
  155.  
  156. }
  157.  
  158. MyBST(E arr[]){
  159. root= sortedArrayToBST(arr);
  160. }
  161. public Node<E> sortedArrayToBST(E[] num) {
  162. if (num.length == 0)
  163. return null;
  164.  
  165. return sortedArrayToBST(num, 0, num.length - 1);
  166. }
  167.  
  168. public Node sortedArrayToBST(E[] num, int start, int end) {
  169. if (start > end)
  170. return null;
  171.  
  172. int mid = (start + end) / 2;
  173. Node root = new Node (num[mid]);
  174. root.left = sortedArrayToBST(num, start, mid - 1);
  175. root.right = sortedArrayToBST(num, mid + 1, end);
  176.  
  177. return root;
  178. }
  179. public void preOrder(){
  180. Stack<Node<E>> s=new Stack<Node<E>>();
  181. if(root==null)
  182. return;
  183. s.push(root);
  184. while (!s.empty()) {
  185. Node<E> n=s.pop();
  186. if(n.right!=null)
  187. s.push(n.right);
  188. if(n.left!=null)
  189. s.push(n.left);
  190. System.out.print(n.data+" ");
  191. }
  192. }
  193. public void inOrder() {
  194. if (root == null) return;
  195.  
  196. Stack<Node<E>> stack = new Stack<Node<E>>();
  197. Node<E> node = root;
  198. while (!stack.isEmpty() || node != null) {
  199. if (node != null) {
  200. stack.add(node);
  201. node = node.left;
  202. } else {
  203. node = stack.pop();
  204. System.out.print(node.data + " ");
  205. node = node.right;
  206. }
  207. }
  208. }
  209.  
  210. public void delete(E dataDeleted)
  211. {
  212. root = delete(root, dataDeleted);
  213. }
  214. private Node<E> delete(Node<E> root, E dataDeleted)
  215. {
  216. if (root == null)
  217. return null;
  218. else if (((Comparable)dataDeleted).compareTo(root.data) < 0)
  219. root.left = delete(root.left, dataDeleted);
  220. else if (((Comparable)dataDeleted).compareTo(root.data) > 0)
  221. root.right = delete (root.right, dataDeleted);
  222. else
  223. { if (root.left == null)
  224. return root.right;
  225. else if(root.right == null)
  226. return root.left;
  227. else
  228. {
  229. // get data from the rightmost node in the left subtree
  230. root.data = root.left.data;
  231. // delete the rightmost node in the left subtree
  232. root.left = delete(root.left, root.data) ;
  233. }
  234. }
  235.  
  236. if(BF(root)==2){
  237. if(BF(root.right)<0){
  238. root.right= RR(root.right);
  239. root=RL(root);
  240. }
  241. else
  242. root=RL(root);
  243. }
  244. if(BF(root)==-2){
  245. if(BF(root.left)>0){
  246. root.left= RL(root.left);
  247. root=RR(root);}
  248. else
  249. root=RR(root);}
  250. return root;
  251. }
  252. //I Used two methods for recursion
  253. public int height()
  254. {
  255. return height(root);
  256. }
  257. private int height(Node<E> myNode)
  258. {
  259. if(myNode == null)
  260. return -1;
  261. else{
  262. return
  263. 1 + Math.max( height(myNode.left), height(myNode.right));
  264. }
  265. }
  266.  
  267. public int BF(Node<E> t){
  268. return height(t.right)-height(t.left);
  269. }
  270. public Node<E> RR(Node<E> x){
  271. Node<E> y=x.left;
  272. x.left=y.right;
  273. y.right=x;
  274. return y;
  275. }
  276. public Node<E> RL(Node<E> x){
  277. Node<E> y=x.right;
  278. x.right=y.left;
  279. y.left=x;
  280. return y;
  281. }
  282. public int count(){
  283. return count(root);
  284. }
  285. public int count(Node<E> root){
  286. if(root==null)
  287. return 0;
  288. return 1+count(root.left)+count(root.right);
  289. }
  290. public void add(E data)
  291. {
  292. root = add(root, data);
  293. }
  294. private Node<E> add(Node<E> root, E data) {
  295. if (root==null||((Comparable)data).compareTo(root.data) == 0){
  296. root= new Node<E>(data);
  297. return root;
  298. }
  299. if (((Comparable)data).compareTo(root.data) < 0)
  300. root.left = add(root.left, data);
  301. else
  302. root.right = add(root.right, data);
  303.  
  304.  
  305. if(BF(root)==2){
  306. if(BF(root.right)<0){
  307. root.right= RR(root.right);
  308. root=RL(root);
  309. }
  310. else
  311. root=RL(root);
  312. }
  313. if(BF(root)==-2){
  314. if(BF(root.left)>0){
  315. root.left= RL(root.left);
  316. root=RR(root);}
  317. else
  318. root=RR(root);}
  319. return root;
  320.  
  321.  
  322.  
  323. }
  324.  
  325.  
  326. //Here we will search for data and return the node for it
  327. public boolean search(E data){
  328. return search(root, data);
  329. }
  330. private boolean search(Node<E> myNode, E data) {
  331. if ( myNode== null)
  332. return false;
  333. else if (((Comparable)data).compareTo( myNode.data)==0){
  334. return true;}
  335. else if (((Comparable)data).compareTo( myNode.data)<0){
  336. return search(myNode.left, data);}
  337. else{
  338. return search(myNode.right, data);}
  339. }
  340.  
  341. public void BreadthFirstRaversal(){
  342. ArrayList<Node<E>> list=new ArrayList<Node<E>>();
  343. list.add(root);
  344. while(list.size()!=0){
  345. Node<E> cNode=list.remove(0);
  346. System.out.print(cNode.data+" ");
  347. if(cNode.left!=null)
  348. list.add(cNode.left);
  349. if(cNode.right!=null)
  350. list.add(cNode.right);
  351. }
  352. }
  353. public boolean hasPathWithSum(int sum){
  354. return hasPathWithSum(root,sum);
  355. }
  356. private boolean hasPathWithSum(Node<E> root,int sum){
  357. if(root==null)
  358. return sum==0;
  359. else
  360. sum=sum-(Integer)root.data;
  361. return
  362. hasPathWithSum(root.left, sum)||hasPathWithSum(root.right, sum);
  363. }
  364. }
  365. ..................................................................................................
  366. public class Node<E> {
  367. E data;
  368. Node<E> left;
  369. Node<E> right;
  370.  
  371.  
  372.  
  373. public Node(E data)
  374. {
  375. this.data=data;
  376. }
  377. public Node(E data, Node<E> Left, Node<E> rgiht)
  378. {
  379. this.left = left;
  380. this.right = rgiht;
  381. this.data = data;
  382. }
  383. public E getData(){
  384. return data;
  385. }
  386. }
  387.  
  388. .
Advertisement
Add Comment
Please, Sign In to add comment