preorderTraversalPrettyTreeView<K> function

String preorderTraversalPrettyTreeView<K>(
  1. AVLTree<K> tree,
  2. {String prefix = "|----",
  3. String postfix = "\n",
  4. bool showStats = false}
)

Pretty print binary tree: DO NOT CHANGE THIS, create one for your own if needed: ex: print(preorderTraversalPrettyTreeView(nodeWithCellDeleted.internalCellTree, prefix:"",postfix:"|", showStats: true ))

Implementation

String preorderTraversalPrettyTreeView<K>(AVLTree<K> tree,
    {String prefix = "|----", String postfix = "\n", bool showStats = false}) {
  var result = "";

  var sb = StringBuffer();

  AVLTreeNode<K> current = tree.root;
  var stack = <AVLTreeNode<K>>[current];
// sb.write(TAB*level);sb.write(current.key.toString());sb.write(NEWLINE);
  int n = 0;
  var space_stack = <int>[n];

  while (stack.isNotEmpty) {
    while (current != null) {
      stack.add(current);
      space_stack.add(n);
      sb.write(prefix * n);
      sb.write(current.key.toString());
      sb.write(postfix);
      current = current.left;
      n++;
    }
    current = stack.removeLast().right;
    n = space_stack.removeLast() + 1;
  }
  if (showStats) {
    var c = tree.size; //claimed size
    var a = AVLTreeAlgos.reCalculateSize(startNode: tree.root); //actual size
    var stats = "Stats: C: $c, A: $a\n";
    result = stats;
  }

  return result + sb.toString();
}