开发者

Binary trees- pseudo codes?

开发者 https://www.devze.com 2023-02-19 02:42 出处:网络
I\'m trying to write this pseudo code for checking whether a data-structure based on trees is a binary tree.

I'm trying to write this pseudo code for checking whether a data-structure based on trees is a binary tree.

I'm having a little problem with understanding this pseudo code form.

This is what I wrote:

Is-Binary(x)    
  if (x=null) {Then Return True
            }
       else {
            if (x.Right<>Null) {Then
                if (x.key<x.right.key){Then
                   Is-Binary(x.Right)}
                else {Return False}}

            if (x.Left<>Null) {Then
               if (x.key>x.Left.key){Then
                  Is-binart(x.Left)}
               else {Return False}}
             }

My question: Can I assume that after the first 开发者_StackOverflowtrue will be accepted, the program wont finish?

What does the return means here? will it sum up all the true/false and give the final soulotion (based on the fact that true*false=false?)

If so, what else can I do?

Thank you


You'll only get one result, either true or false, because you're not accumulating anything. Assuming you're starting from the top of a tree, say you only go one level deep, you'll only get true or false as a result. Then, if you get another level deeper (with another call), you're just facing the same possibilities: True, false, or deeper in the tree.

[Edit, after further observation:] Unless I'm mistaken, you'll get True the first time you hit a null, which might never happen because you never call Is-Binary on a null-value.

So if X is null you get true, else you get false.

0

精彩评论

暂无评论...
验证码 换一张
取 消