who can help me? about recursion in a binary tree

kathleen 2001-07-27 06:01:15
use a display method to show the tree structure, such like this:
Karen
Kathy
Ashley
Tom
null
Jane
Tim
Tony
my code(in Java)is:
void display(BinaryTreeNode tempRoot) {
//String space2="";

System.out.println(tempRoot);

space2=space2+" ";
//if the current root has no child, do nothing.
if (tempRoot.leftChild==null && tempRoot.rightChild==null) {
return;
}
if (tempRoot.leftChild!=null) {
//space1=space1+" ";
System.out.print(space2);
display(tempRoot.leftChild);

}
if (tempRoot.rightChild!=null) {
//space2=space2+" ";
System.out.print(space2);
display(tempRoot.rightChild);
}
}

I really don't know how to deal with the variable space2 and let
it increase according to the grade in the recursion method.
The problem is:
1.if I delare space2 as local variable in the method, every time the method calls itself,the space2 is innitialized. so the result
like this(no indention):
Karen
Kathy
Tony
2.if I declare space2 as class variable, every time the method calls itself, the space2 is increased by 3 spaces, the indention is
not right like this:
Karen
Kathy
Tony
who can solve it, I will add the score immediatly

...全文
82 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kathleen 2001-07-28
  • 打赏
  • 举报
回复
thanks starfish. I figured it out last night, but I used about 16 sentence. yours is much simpler. It seems I have to put much energy on the efficency of code.
starfish 2001-07-27
  • 打赏
  • 举报
回复
作为参数传递就不就可以了么,很简单呀。

void display(BinaryTreeNode tempRoot, string space) {
if (tempRoot==null) return;
System.out.print(space);
System.out.println(tempRoot);
display(tempRoot.leftChild, space+" ");
display(tempRoot.rightChild, space+" ");
}
Everyday Data Structures by William Smith English | 14 Mar. 2017 | ASIN: B01M01YOVF | 344 Pages | AZW3 | 1.4 MB A practical guide to learning data structures simply and easily About This Book This book is a very practical, friendly, and useful guide that will help you analyze problems and choose the right data structures for your solution Learn to recognize data patterns for determining which structures apply to a given problem Explore the unique rules or "gotchas" that will help you become an excellent programmer Who This Book Is For If you're self-taught programmers in any language who wants to gain a solid understanding of data structures and how to use them to solve real-world problems in your day-to-day development work, then this book is for you. What You Will Learn A rapid overview of data types, applications for each type, best practices and high-level variations between platforms Review the most common data structures and build working examples in the languages used for mobile platform software development Understand advanced data structure concepts such as generic collections, searching and sorting algorithms, and recursion Learn to use Stacks (LIFO) and queues (FIFO) in your daily application Add/remove objects and nest arrays and dictionaries within another dictionary and understand why such architecture is often preferred or necessary Get acquainted with the tree structures such as heap, binary, and graphs, apply them to work Unleash the power of different sorting techniques such as bubble sort, quick sort, merge sort, insertion sort, and radix sort Perform searching operations on arrays, heaps, graphs, and binary trees in different languages In Detail If you want to learn different data structures and their real-world applications quickly through practical examples, then Everyday Data Structures is for you. This book can introduce you to new data structures and their potential applications through examples in languages common to mobile software development on the most popular platforms. The examples are presented with real-world concepts using language that everyone will understand. This book is logically divided into two parts; the first one covers the basic data structures that are built into most languages such as Objective-C, C#, Java, and Swift. It will cover detailed analysis of the common data structures such as arrays, lists, stacks, Queues, and heaps, typical applications, and specific concerns for each language. Each chapter will provide in-depth examples in several popular languages based on real-world applications. The second part will cover more advanced data structures such as generic collections, sorting, searching, and recursion and ways to use those structures in everyday applications. Style and approach This is a practical, result-focused guide, which is easy to follow, but also fast-paced and really satisfying with full of examples.

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧