hqt

Binary Search Tree

hqt
Jul 18th, 2013
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package com.DTT;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Queue;
  7.  
  8. public class BinarySearchTree {
  9.  
  10.     public static class Node {
  11.         int value;
  12.         Node left;
  13.         Node right;
  14.        
  15.         public Node(int value) {
  16.             this.value = value;
  17.             left = null;
  18.             right = null;
  19.         }
  20.     }
  21.    
  22.     Node root = null;
  23.    
  24.     public void insert(int value) {
  25.         root = insert(root, value);
  26.     }
  27.    
  28.     private Node insert(Node root, int value) {
  29.         if (root == null)
  30.             return new Node(value);
  31.         if (value <= root.value)
  32.             root.left = insert(root.left, value);
  33.         else
  34.             root.right = insert(root.right, value);
  35.         return root;
  36.     }
  37.     public void inorderTraversal(Node root) {
  38.         if (root == null) return;
  39.         if (root.left != null) inorderTraversal(root.left);
  40.         System.out.print(root.value + " ->");
  41.         if (root.right != null) inorderTraversal(root.right);
  42.     }
  43.    
  44.     public void preorderTraversal(Node root) {
  45.         if (root == null) return;
  46.         System.out.print(root.value + "->");
  47.         if (root.left != null) preorderTraversal(root.left);
  48.         if (root.right != null) preorderTraversal(root.right);
  49.     }
  50.    
  51.     public void postorderTraversal(Node root) {
  52.         if (root == null) return;
  53.         if (root.left != null) postorderTraversal(root.left);
  54.         if (root.right != null) postorderTraversal(root.right);
  55.         System.out.print(root.value + "->");
  56.     }
  57.    
  58.    
  59.     private void FindDistance(Node root, int small, int big) {
  60.         if (root == null) return;
  61.         List<Node> p1 = new ArrayList<Node>();
  62.         List<Node> p2 = new ArrayList<Node>();
  63.         boolean res1 = true;
  64.         boolean res2 = true;
  65.         if (big == root.value) {
  66.             res1 &= FindPath(root.left, small, p1);
  67.             p2.add(root);
  68.         }
  69.         else if (small == root.value) {
  70.             p1.add(root);
  71.             res2 &= FindPath(root.right, big, p2);
  72.         }
  73.         else if (big < root.value) FindDistance(root.left, small, big);
  74.         else if (small > root.value) FindDistance(root.right, small, big);
  75.         else if (small < root.value && big > root.value) {
  76.             p1.add(root);
  77.             res1 &= FindPath(root.left, small, p1);
  78.             res2 &= FindPath(root.right, big, p2);
  79.         }
  80.        
  81.         if (!res1 || !res2) {
  82.             System.out.println("does not contain this node in binary search tree");
  83.             return;
  84.         }
  85.         // print
  86.         for (int i = p1.size() - 1; i >= 0; i--) System.out.print(p1.get(i).value + "->");
  87.         for (int i = 0;i < p2.size() - 1; i++) System.out.print(p2.get(i).value + "->");
  88.         System.out.println(p2.get(p2.size() - 1).value);
  89.     }
  90.          
  91.          
  92.     private boolean FindPath(Node root, int val, List<Node> path) {
  93.         if (root == null) return false;
  94.         path.add(root);
  95.         if (root.value == val) return true;
  96.         if (root.value > val) return FindPath(root.left, val, path);
  97.         else return FindPath(root.right, val, path);
  98.     }
  99.          
  100.     private void FindDistanceNormalize(int small, int big) {
  101.         FindDistance(root, small, big);
  102.     }
  103.    
  104.     public void FindDistance(int a, int b) {
  105.         if (a > b) FindDistanceNormalize(b, a);
  106.         else FindDistanceNormalize(b, a);
  107.     }
  108.          
  109.    
  110.     public static void main(String[] args) {
  111.         BinarySearchTree tree = new BinarySearchTree();
  112.         tree.insert(9);
  113.         tree.insert(3);
  114.         tree.insert(16);
  115.         tree.insert(2);
  116.         tree.insert(5);
  117.         tree.insert(14);
  118.         tree.insert(18);
  119.         tree.insert(4);
  120.         tree.insert(8);
  121.        
  122.         System.out.println("Distance:");
  123.         tree.FindDistance(14, 4);
  124.        
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment