Find loop in binary tree

How to find a loop in a binary tree? I am looking for a solution other than marking visited sites as visited, or for hashing addresses. Any ideas?

+5
source share
4 answers

Suppose that you have a binary tree, but you do not trust it, and you think that it can be a graph, the general case will dictate to remember the visited nodes. To some extent, this is the same algorithm as building a minimal spanning tree from a graph, and this means that the problem with space and time will be a problem.

Another approach would be to look at the data that you save in the tree. You have a number of hashes so you can compare.

Pseudocode will check these conditions:

  • node 2 1 ( 3 ). 3 = > .
  • .
  • node , , , . , , node node (, ), . node, , , , , .

    3.a. , , , . So = > . .

, , ? , node . . , , , .

+2

, . :

+2

: ( ) ().

, ( , ), node ( , ), node, . - google-.

+1

Binary trees do not contain cycles. If they do, these are not trees in the first place.

0
source

All Articles