forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeToList.java
74 lines (59 loc) · 2.04 KB
/
TreeToList.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinarySearchTree;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 4:38 PM
*/
public class TreeToList<E extends Comparable<E>> extends BinarySearchTree<E> {
/**
* A recursive function that takes an ordered binary tree
* and rearranges the internal pointers to make a circular
* doubly linked list out of the tree nodes. The list should
* be arranged so that the nodes are in increasing order.
*/
public void treeToList() {
// print the list
printList(treeToList(root));
}
public static <E extends Comparable<E>> BinaryNode<E> treeToList(BinaryNode<E> node) {
if (node == null) return null;
BinaryNode<E> aList = treeToList(node.left);
BinaryNode<E> bList = treeToList(node.right);
node.left = node;
node.right = node;
// attach left child then root followed by right child (so that final list is in ascending order)
aList = addToList(aList, node);
aList = addToList(aList, bList);
return aList;
}
private static <E extends Comparable<E>> BinaryNode<E> addToList(BinaryNode<E> aList, BinaryNode<E> bList) {
if (aList == null) return bList;
if (bList == null) return aList;
// find the last node in each list
BinaryNode<E> aListLast = aList.left;
BinaryNode<E> bListLast = bList.left;
// join end of one list to beginning of another
aListLast.right = bList;
bList.left = aListLast;
// make circular
aListLast.left = bListLast;
bListLast.right = aList;
return aList;
}
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
bst.put(6);
bst.put(3);
bst.put(5);
bst.put(7);
bst.put(8);
bst.put(9);
bst.inOrder();
// TODO incorrect results
bst.printList(treeToList(bst.root));
}
}