|
| 1 | + |
| 2 | +/* |
| 3 | + * Question: Convert a binary search tree to a circular doubly-linked list |
| 4 | + * |
| 5 | + * Source: http://www.careercup.com/question?id=5156120807079936 |
| 6 | + * |
| 7 | + * Algorithm: 1. Visit every node using in-order traversal, In every visit keep track of previous node |
| 8 | + * 2. Make previous.next = current |
| 9 | + * Make current.previous = previous |
| 10 | + */ |
| 11 | + |
| 12 | +package BSTToCircularDLL; |
| 13 | + |
| 14 | +public class UsingInorderTraversal { |
| 15 | + |
| 16 | + static Node prev,head; |
| 17 | +public static void main(String[] args) { |
| 18 | + BST bst=BinSearchTree.makeTree(); |
| 19 | + convertBSTToCircularDLL(bst.root); |
| 20 | + //printDLL(head); |
| 21 | +} |
| 22 | + |
| 23 | +/*private static void printDLL(Node n) { |
| 24 | + |
| 25 | + while(n!=null){ |
| 26 | + System.out.print(n.data+" "); |
| 27 | + n=n.rchild; |
| 28 | + } |
| 29 | +} |
| 30 | +*/ |
| 31 | +private static void convertBSTToCircularDLL(Node n) { |
| 32 | + if(n==null) |
| 33 | + return; |
| 34 | + convertBSTToCircularDLL(n.lchild); |
| 35 | + if(prev==null){ // first node in list |
| 36 | + head=n; |
| 37 | + } |
| 38 | + else{ |
| 39 | + prev.rchild = n; |
| 40 | + n.lchild = prev; |
| 41 | + } |
| 42 | + prev = n; |
| 43 | + convertBSTToCircularDLL(n.rchild); |
| 44 | + if(prev.rchild==null){ // last node in list |
| 45 | + head.lchild = prev; |
| 46 | + prev.rchild = head; |
| 47 | + } |
| 48 | +} |
| 49 | +/* |
| 50 | + * Analysis: |
| 51 | + * Time Complexity = O(n) where n = number of nodes in the BST |
| 52 | + * Space Complexity = O(1) |
| 53 | + */ |
| 54 | + |
| 55 | +} |
| 56 | +class Node{ |
| 57 | + public int data; |
| 58 | + public Node lchild; // Note that: for DLL, left will act as Previous Node |
| 59 | + public Node rchild; // Note that: for DLL, right will act as Next Node |
| 60 | + |
| 61 | + public Node(int data) { |
| 62 | + this.data=data; |
| 63 | + lchild=null; |
| 64 | + rchild=null; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class BST { |
| 69 | +public Node root=null; |
| 70 | + |
| 71 | + public void addNode(Node n){ |
| 72 | + if(root==null) |
| 73 | + { |
| 74 | + root=n; |
| 75 | + System.out.println(n.data+" added as the root to the tree"); |
| 76 | + } |
| 77 | + else{ |
| 78 | + |
| 79 | + Node current=root; |
| 80 | + Node parent; |
| 81 | + while(true){ |
| 82 | + parent=current; |
| 83 | + |
| 84 | + if(n.data<current.data){ |
| 85 | + current=current.lchild; |
| 86 | + if(current==null){ |
| 87 | + parent.lchild=n; |
| 88 | + return; |
| 89 | + } |
| 90 | + } |
| 91 | + else{ |
| 92 | + current=current.rchild; |
| 93 | + if(current==null){ |
| 94 | + parent.rchild=n; |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + public void display(){ |
| 104 | + display(root); |
| 105 | + System.out.println(); |
| 106 | + } |
| 107 | + |
| 108 | + public void display(Node n){ |
| 109 | + Node tempNode=n; |
| 110 | + if(tempNode==null) |
| 111 | + return; |
| 112 | + else{ |
| 113 | + display(tempNode.lchild); |
| 114 | + System.out.print(tempNode.data+" "); |
| 115 | + display(tempNode.rchild); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | +class BinSearchTree{ |
| 121 | + |
| 122 | + public static BST makeTree() { |
| 123 | + BST myTree=new BST(); |
| 124 | + System.out.println("Adding the elemets"); |
| 125 | + myTree.addNode(new Node(40)); |
| 126 | + myTree.addNode(new Node(50)); |
| 127 | + myTree.addNode(new Node(30)); |
| 128 | + myTree.addNode(new Node(80)); |
| 129 | + myTree.addNode(new Node(20)); |
| 130 | + myTree.addNode(new Node(10)); |
| 131 | + myTree.addNode(new Node(5)); |
| 132 | + myTree.addNode(new Node(85)); |
| 133 | + myTree.addNode(new Node(35)); |
| 134 | + myTree.addNode(new Node(45)); |
| 135 | + myTree.addNode(new Node(32)); |
| 136 | + myTree.addNode(new Node(15)); |
| 137 | + myTree.addNode(new Node(38)); |
| 138 | + myTree.addNode(new Node(36)); |
| 139 | + myTree.display(); |
| 140 | + |
| 141 | + return myTree; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | + |
0 commit comments