|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.LinkedList; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class treeclass { |
| 6 | + private class Node { |
| 7 | + int data; |
| 8 | + ArrayList<Node> child = new ArrayList<>(); |
| 9 | + } |
| 10 | + |
| 11 | + private Node root; |
| 12 | + private int size; |
| 13 | + |
| 14 | + /* |
| 15 | + A generic tree is a tree which can have as many children as it can be |
| 16 | + It might be possible that every node present is directly connected to |
| 17 | + root node. |
| 18 | +
|
| 19 | + In this code |
| 20 | + Every function has two copies: one function is helper function which can be called from |
| 21 | + main and from that function a private function is called which will do the actual work. |
| 22 | + I have done this, while calling from main one have to give minimum parameters. |
| 23 | +
|
| 24 | + */ |
| 25 | + public treeclass() { //Constructor |
| 26 | + Scanner scn = new Scanner(System.in); |
| 27 | + root = create_treeG(null, 0, scn); |
| 28 | + } |
| 29 | + |
| 30 | + private Node create_treeG(Node node, int childindx, Scanner scn) { |
| 31 | + // display |
| 32 | + if (node == null) { |
| 33 | + System.out.println("Enter root's data"); |
| 34 | + } else { |
| 35 | + System.out.println("Enter data of parent of index " + node.data + " " + childindx); |
| 36 | + } |
| 37 | + // input |
| 38 | + node = new Node(); |
| 39 | + node.data = scn.nextInt(); |
| 40 | + System.out.println("number of children"); |
| 41 | + int number = scn.nextInt(); |
| 42 | + for (int i = 0; i < number; i++) { |
| 43 | + Node childd = create_treeG(node, i, scn); |
| 44 | + size++; |
| 45 | + node.child.add(childd); |
| 46 | + } |
| 47 | + return node; |
| 48 | + } |
| 49 | + |
| 50 | + /* |
| 51 | + Function to display the generic tree |
| 52 | + */ |
| 53 | + public void display() { //Helper function |
| 54 | + display_1(root); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + private void display_1(Node parent) { |
| 59 | + System.out.print(parent.data + "=>"); |
| 60 | + for (int i = 0; i < parent.child.size(); i++) { |
| 61 | + System.out.print(parent.child.get(i).data + " "); |
| 62 | + } |
| 63 | + System.out.println("."); |
| 64 | + for (int i = 0; i < parent.child.size(); i++) { |
| 65 | + display_1(parent.child.get(i)); |
| 66 | + } |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + /* |
| 71 | + One call store the size directly but if you are asked compute size this function to calcuate |
| 72 | + size goes as follows |
| 73 | + */ |
| 74 | + |
| 75 | + public int size2call() { |
| 76 | + return size2(root); |
| 77 | + } |
| 78 | + |
| 79 | + public int size2(Node roott) { |
| 80 | + int sz = 0; |
| 81 | + for (int i = 0; i < roott.child.size(); i++) { |
| 82 | + sz += size2(roott.child.get(i)); |
| 83 | + } |
| 84 | + return sz + 1; |
| 85 | + } |
| 86 | + |
| 87 | + /* |
| 88 | + Function to compute maximum value in the generic tree |
| 89 | + */ |
| 90 | + public int maxcall() { |
| 91 | + int maxi = root.data; |
| 92 | + return max(root, maxi); |
| 93 | + } |
| 94 | + |
| 95 | + private int max(Node roott, int maxi) { |
| 96 | + if (maxi < roott.data) |
| 97 | + maxi = roott.data; |
| 98 | + for (int i = 0; i < roott.child.size(); i++) { |
| 99 | + maxi = max(roott.child.get(i), maxi); |
| 100 | + } |
| 101 | + |
| 102 | + return maxi; |
| 103 | + } |
| 104 | + |
| 105 | + /* |
| 106 | + Function to compute HEIGHT of the generic tree |
| 107 | + */ |
| 108 | + |
| 109 | + public int heightcall() { |
| 110 | + return height(root) - 1; |
| 111 | + } |
| 112 | + |
| 113 | + private int height(Node node) { |
| 114 | + int h = 0; |
| 115 | + for (int i = 0; i < node.child.size(); i++) { |
| 116 | + int k = height(node.child.get(i)); |
| 117 | + if (k > h) |
| 118 | + h = k; |
| 119 | + } |
| 120 | + return h + 1; |
| 121 | + } |
| 122 | + |
| 123 | + /* |
| 124 | + Function to find whether a number is present in the generic tree or not |
| 125 | + */ |
| 126 | + |
| 127 | + public boolean findcall(int info) { |
| 128 | + return find(root, info); |
| 129 | + } |
| 130 | + |
| 131 | + private boolean find(Node node, int info) { |
| 132 | + if (node.data == info) |
| 133 | + return true; |
| 134 | + for (int i = 0; i < node.child.size(); i++) { |
| 135 | + if (find(node.child.get(i), info)) |
| 136 | + return true; |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + /* |
| 142 | + Function to calculate depth of generic tree |
| 143 | + */ |
| 144 | + public void depthcaller(int dep) { |
| 145 | + depth(root, dep); |
| 146 | + } |
| 147 | + |
| 148 | + public void depth(Node node, int dep) { |
| 149 | + if (dep == 0) { |
| 150 | + System.out.println(node.data); |
| 151 | + return; |
| 152 | + } |
| 153 | + for (int i = 0; i < node.child.size(); i++) |
| 154 | + depth(node.child.get(i), dep - 1); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + /* |
| 159 | + Function to print generic tree in pre-order |
| 160 | + */ |
| 161 | + public void preordercall() { |
| 162 | + preorder(root); |
| 163 | + System.out.println("."); |
| 164 | + } |
| 165 | + |
| 166 | + private void preorder(Node node) { |
| 167 | + System.out.print(node.data + " "); |
| 168 | + for (int i = 0; i < node.child.size(); i++) |
| 169 | + preorder(node.child.get(i)); |
| 170 | + } |
| 171 | + |
| 172 | + /* |
| 173 | + Function to print generic tree in post-order |
| 174 | + */ |
| 175 | + public void postordercall() { |
| 176 | + postorder(root); |
| 177 | + System.out.println("."); |
| 178 | + } |
| 179 | + |
| 180 | + private void postorder(Node node) { |
| 181 | + for (int i = 0; i < node.child.size(); i++) |
| 182 | + postorder(node.child.get(i)); |
| 183 | + System.out.print(node.data + " "); |
| 184 | + } |
| 185 | + |
| 186 | + /* |
| 187 | + Function to print generic tree in level-order |
| 188 | + */ |
| 189 | + |
| 190 | + public void levelorder() { |
| 191 | + LinkedList<Node> q = new LinkedList<>(); |
| 192 | + q.addLast(root); |
| 193 | + while (!q.isEmpty()) { |
| 194 | + int k = q.getFirst().data; |
| 195 | + System.out.print(k + " "); |
| 196 | + |
| 197 | + for (int i = 0; i < q.getFirst().child.size(); i++) { |
| 198 | + q.addLast(q.getFirst().child.get(i)); |
| 199 | + } |
| 200 | + q.removeFirst(); |
| 201 | + } |
| 202 | + System.out.println("."); |
| 203 | + } |
| 204 | + |
| 205 | + /* |
| 206 | + Function to remove all leaves of generic tree |
| 207 | + */ |
| 208 | + public void removeleavescall() { |
| 209 | + removeleaves(root); |
| 210 | + } |
| 211 | + |
| 212 | + private void removeleaves(Node node) { |
| 213 | + ArrayList<Integer> arr = new ArrayList<>(); |
| 214 | + for (int i = 0; i < node.child.size(); i++) { |
| 215 | + if (node.child.get(i).child.size() == 0) { |
| 216 | + arr.add(i); |
| 217 | + // node.child.remove(i); |
| 218 | + // i--; |
| 219 | + } else |
| 220 | + removeleaves(node.child.get(i)); |
| 221 | + } |
| 222 | + for (int i = arr.size() - 1; i >= 0; i--) { |
| 223 | + node.child.remove(arr.get(i) + 0); |
| 224 | + } |
| 225 | + } |
| 226 | + |
0 commit comments