When I do binary search, I use a recursive local function to deal with the elements in the tree. but in most of cases, it is not as efficient as a non-local function version. I'm not sure why is it happend. Is local function more consuming?
local function version:
internal void Next2(Action<int?> sProcess)
{//
var Process = sProcess;
Next3(this);
#region Local func
void Next3(Node L) {
if (L.LeftChild == null && L.RightChild == null) {
Process?.Invoke(this.Element);
}
else {
if (L.LeftChild != null) {
Next3(L.LeftChild);
}
if (L.RightChild != null) {
Next3(L.RightChild);
}
}
}
#endregion
}
non-local function version:
internal void Next(Action<int?> Process) //
{//[[this!!]]
if (LeftChild == null && RightChild == null) {
Process?.Invoke(this.Element);
}
else {
if (LeftChild != null) {
this.LeftChild.Next(Process);
}
if (RightChild != null) {
this.RightChild.Next(Process);
}
}
}