Conversion of recursive algorithm and cyclic algorithm

xiaoxiao2021-03-06  43

Recovering algorithm and conversion of cyclic algorithms.

Such as a loop implementation for finding a number in the interpolation tree:

Node FINDSIXNODE (Node root) {

Node Curnode = root;

While (curNode) {

IF (Curnode.getValue () == 6) Return Curnode;

Else IF (curnode.getvalue () <6) Curnode = CURNODE.GETRIGHT ();

Else IF (Curnode.getValue ()> 6) Curnode = CURNODE.GETLEFT ();

}

Return NULL;

}

Recursive implementation:

Node FINDSIXNODE (Node root) {

IF (! root) return null;

Else IF (Root.getValue () == 6) Return root;

Else IF (Root.getValue () <6) Return Findsix (Root.Getright ());

Else IF (Root.getValue ()> 6) Return Findsix (Root.getLeft ());

}

}

转载请注明原文地址:https://www.9cbs.com/read-85659.html

New Post(0)