forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleTree.java
49 lines (41 loc) · 1.18 KB
/
DoubleTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinaryTree;
import static java.lang.System.out;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 6:02 PM
*/
public class DoubleTree {
/**
* Converts a given tree to its Double tree. To create a Double tree
* of the given tree, create a new duplicate for each node, and insert
* the duplicate as the left child of the original node.
*
* @param node
*/
public static <E extends Comparable<E>> void doubleTree(BinaryNode<E> node) {
if (node == null) return;
BinaryNode<E> newNode = new BinaryNode<>(node.value, node.left, null);
node.left = newNode;
doubleTree(newNode.left);
doubleTree(node.right);
}
public static void main(String[] args) {
BinaryTree<Integer> bt = new BinaryTree<>();
bt.put(6);
bt.put(3);
bt.put(5);
bt.put(7);
bt.put(8);
bt.put(9);
bt.breadthFirstTraversal();
out.println();
doubleTree(bt.root);
out.println("BFS after Double tree: ");
bt.breadthFirstTraversal();
}
}