递归 [英] recursive

查看:59
本文介绍了递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个b.tree,它有一个像这样的前缀表达式 - > + x 2 - 5 1 x 3

2


如果我使用它打印的这个递归函数((2x(5-1))+(3x2))


void printTree(t)

if(t.left != NULL)

print("(");

printTree(t.left);

print(t.element) ;

if(t.right!= NULL)

printTree(t.right);

print(")");

你能解释它为什么打印((2x(5-1))+(3x2))

i have a b.tree that has a prefix expression like this->+ x 2 - 5 1 x 3
2

if i use this recursive function it prints ((2x(5-1))+(3x2))

void printTree(t)
if(t.left!=NULL)
print("(");
printTree(t.left);
print(t.element);
if(t.right!=NULL)
printTree(t.right);
print(")");
can you explain why it prints ((2x(5-1))+(3x2))

推荐答案

jw写道:
我有一个b.tree有一个像这样的前缀表达式 - > + x 2 - 5 1 x 3
2


你的b.tree怎么样? _have_那个表达式?

如果我使用它打印的这个递归函数((2x(5-1))+(3x2))

void printTree(t)
if(t.left!= NULL)
print("(");
printTree(t.left);
print(t.element);
if (t.right!= NULL)
printTree(t.right);
print(")");
你能解释它打印的原因((2x(5-1)) )+(3x2))
i have a b.tree that has a prefix expression like this->+ x 2 - 5 1 x 3
2
How does your "b.tree" _have_ that expression?
if i use this recursive function it prints ((2x(5-1))+(3x2))

void printTree(t)
if(t.left!=NULL)
print("(");
printTree(t.left);
print(t.element);
if(t.right!=NULL)
printTree(t.right);
print(")");
can you explain why it prints ((2x(5-1))+(3x2))




它显示了要求打印的内容。既然你没有显示你从前缀表达式形成树的方式是什么,那么就无法判断这个函数是否真正做了正确的事。




V



It prints what it''s asked to print, apparently. Since you don''t show how
you form the tree from your prefix expression, there is no way to tell if
the function actually does the right thing or not.

V


root = +

root-> right = x x-> left = 3 x-> right = 2

root-> left = x x-> left = 2 x-> right = - - - > left = 5

- - > right1

root=+
root->right=x x->left=3 x->right=2
root->left=x x->left=2 x->right=- - ->left=5
- ->right1


jw写道:
root = +
root-> right = x x-> left = 3 x-> right = 2
root-> left = x x-> left = 2 x-> right = - - - > left = 5
- - > right1
root=+
root->right=x x->left=3 x->right=2
root->left=x x->left=2 x->right=- - ->left=5
- ->right1




所以,我得到它,就像这样


。 。[+]

.. / \

.. [x] [x]

.. / \ / \

.. [2] [ - ] [3 ] [2]

.. / \

.. [5] [1]


应该打印什么?首先它需要根,对吧?然后它看到左边的'
(''''')不是NULL。因此,它首先打印括号,然后是左边的树,然后是
。怎么样?左边打印树是什么意思

''x''现在是临时根?它看着它的左边。它不是NULL(它是最左边'2''的
),所以打印另一个括号,然后是子树

最左边的' '2''是新根。怎么样?它的左边是NULL,它的元素''是'2',它的右边是NULL。然后它返回。哪里?要打印左边的x子树的剩余部分,需要打印
。怎么样?它打印元素,它是'b $ b" x",然后它看到右边不是NULL。它打印正确的子树

(以' - ''为根)。怎么样? ...


继续,直到你完成打印'+''的右边部分。


我什么也看不见顺便问一下C ++的问题。下次发布到新闻组

,你的一般编程问题更加热门,编程。


V



So, as I get it, it''s like this

.. [ + ]
.. / \
.. [ x ] [ x ]
.. / \ / \
.. [ 2 ] [ - ] [ 3 ] [ 2 ]
.. / \
.. [ 5 ] [ 1 ]

What should it print? First it takes the root, right? Then it sees that
the left (the ''x'') is not NULL. So, it prints the parenthesis first, and
then the left tree. How? What does it mean to print a tree when the left
''x'' is now a temporary root? It looks at its left. It''s not NULL (it''s
the leftmost ''2''), so it prints another parenthesis, and then the subtree
where the leftmost ''2'' is the new root. How? Its left is NULL, its
''element'' is "2", and its right is NULL. Then it returns. Where? To
print the rest of the left ''x'' subtree. How? It prints the element, it''s
"x", then it see that the right is not NULL. It prints the right subtree
(with the ''-'' as its root). How? ...

Continue until you finish printing the right part of the ''+''.

I don''t see any C++ question, by the way. Next time post to a newsgroup
where your general programming question is more topical, comp.programming.

V


这篇关于递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆