[12909] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Diagnostics.Contracts;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading.Tasks;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
|
---|
[12923] | 9 | internal class SearchTree<TValue> {
|
---|
[12924] | 10 | private class Node {
|
---|
[12923] | 11 | internal TValue value;
|
---|
[12924] | 12 | internal Node parent;
|
---|
| 13 | internal Node[] children;
|
---|
[12909] | 14 | // children == null -> never visited
|
---|
| 15 | // children[i] != null -> visited at least once, still allowed
|
---|
| 16 | // children[i] = null -> fully explored
|
---|
| 17 | }
|
---|
| 18 |
|
---|
[12924] | 19 | private Node root;
|
---|
[12909] | 20 |
|
---|
| 21 | // for iteration
|
---|
[12924] | 22 | private Node currentNode;
|
---|
[12909] | 23 |
|
---|
| 24 | public SearchTree() {
|
---|
[12924] | 25 | root = new Node();
|
---|
[12909] | 26 | currentNode = root;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public void ResetPosition() {
|
---|
| 30 | currentNode = root;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public bool IsLeafNode() {
|
---|
| 34 | return currentNode.children == null;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[12923] | 37 | public void ExpandCurrentNode(IEnumerable<TValue> values) {
|
---|
| 38 | Contract.Assert(values.Any());
|
---|
[12909] | 39 | Contract.Assert(currentNode.children == null);
|
---|
[12924] | 40 | currentNode.children = values.Select(val => new Node() { value = val, parent = currentNode }).ToArray();
|
---|
[12909] | 41 | }
|
---|
| 42 |
|
---|
[12923] | 43 | public void Follow(TValue value) {
|
---|
| 44 | // TODO: perf
|
---|
| 45 | int i = 0;
|
---|
| 46 | while (i < currentNode.children.Length && (
|
---|
| 47 | currentNode.children[i] == null || !currentNode.children[i].value.Equals(value))) i++;
|
---|
| 48 |
|
---|
| 49 | if (i >= currentNode.children.Length) throw new InvalidProgramException();
|
---|
| 50 | currentNode = currentNode.children[i];
|
---|
[12909] | 51 | }
|
---|
| 52 |
|
---|
[12923] | 53 | public IEnumerable<TValue> ChildValues {
|
---|
[12909] | 54 | get {
|
---|
[12923] | 55 | return from ch in currentNode.children
|
---|
| 56 | where ch != null
|
---|
| 57 | select ch.value;
|
---|
[12909] | 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[12923] | 61 | public void RemoveBranch(TValue value) {
|
---|
| 62 | // TODO: perf
|
---|
| 63 | int i = 0;
|
---|
| 64 | while (i < currentNode.children.Length && (
|
---|
| 65 | currentNode.children[i] == null || !currentNode.children[i].value.Equals(value))) i++;
|
---|
[12909] | 66 |
|
---|
[12923] | 67 | if (i >= currentNode.children.Length) throw new InvalidProgramException();
|
---|
| 68 | currentNode.children[i] = null;
|
---|
| 69 |
|
---|
[12909] | 70 | RemoveRecursively(currentNode);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[12924] | 73 | private void RemoveRecursively(Node node) {
|
---|
[12909] | 74 | // when the last child has been removed we must remove the current node from it's parent
|
---|
[12922] | 75 | while (node.parent != null && node.children.All(ch => ch == null)) {
|
---|
[12909] | 76 | node.parent.children[Array.IndexOf(node.parent.children, node)] = null;
|
---|
[12922] | 77 | node = node.parent;
|
---|
[12909] | 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|