我打算发一下自己对于算法导论书里面的个人答案。

欢迎大家指正。

因为读的是英文版,这里也用英文来发吧:)

12.1 What is a binary search tree

12.1-1: 

12.1-2:

BSP Property: Let x be a node in BST, if y is a node in the left subtree of x, then:

 key[x]>= key[y]; if y is a node on the right subtree of x, then key[x]<=key[y].

Min-Heap Property: for any node x, key[parent[x]] >= key[x].

Min-Heap cannot print these n nodes in sorted order with time O(n), since after

listing the maximum, the update of that that heap takes time O(h), where h is the

height of the heap.

12.1-3:

Stack Method:

代码
/* Basic Idea:
* when outputing, for one single node, it has 4 basic conditions:
* 1. no child visited
* 2. left child visited
* 3. result outputed
* 4. right child visited
*
* However, it can be simplified to:
* 1. visit-left child directly
* 2. out put the result, then visit the right child
*
* I here make use of this simplification, and use whether it's in the
* stack to differenciate these two conditions.
*/
NON
-RECURSIVE-INORDER-TREE-WALK-WITH-STACK(root)
// assume s is a stack
s.push(null); // set the stack to be non-empty first,
// in case a when visiting the right child of
// the root, stack s is empty therefore the
// while loop terminates
current = root;
while(!s.empty())
if(current==null):
// two possibiltiies:
// 1. current is the left end
// -- then go back to its parrent (upper level key)
// stored, output it, and set current to be
// the right child
// 2. current is the right end
// -- then go back to the next station, output it...
// under each of these condition, should all do these:
current = s.poll();
output current.key;
current
= current.right;
else:
// the case when it's not null, therefore store that to
// the stack
s.push(current);
current
= current.left;
end of
while loop

Here's the Java Code:

 

(Based on an AVL Tree I've implemented before)

代码
publicvoid iterTreeWalk(Node x){
LinkedList
<Node> s =new LinkedList<Node>();
Node current
= x;s.push(null);
System.out.println(
"The Current Tree is: ");
while(!s.isEmpty()){
if(current==null){
current
= s.poll();
if(!s.isEmpty()){
System.out.print(current.value
+", ");
current
= current.right;
}

}
else{
s.push(current);
current
= current.left;
}
}
}

Non-Stack Method:

 

代码
NON-RECURSIVE-INORDER-TREE-WALK-WITHOUT-STACK(x):
// find the least value first
while(x.left !=null):
x
= x.left;
// then find the successor of x iteratively
do:  // go and find the successor
output x.key;
// the successor found in the last loop
if(right[x] !=null):
x
= x.right;
while(x.left!=null)
x
=x.left;
else:
p
= x.parent;
while(p !=null&& x == p.right):
x
= p; p = p.parent;
       x = p; // forgot this initially
while(x !=null);

 Java Code:

 

代码
publicvoid iterTreeWalkWithoutStack(Node x){
Node p
=null;

// go to the minimum first: x = findMin(x);
while(x.left!=null) x = x.left;

// then find the successor recursively
do{
// output the value first
System.out.print(x.value+", ");

// then find the successor of x: x = findSuccessor(x);
if(x.right !=null){ // find the minimum of right sub-tree
x = x.right;
while(x.left!=null) x = x.left;
}
else{
p
= x.parent;
while(p !=null&& x==p.right){
x
= p; p = p.parent;
}
x
=p;
}
}
while(x!=null);
System.out.println();
}

 

 

 

 12.4:

PREORDER-TREE-WALK(x):

if(x!=null):

output x.key;

PREORDER-TREE-WALK(x.left);

PREORDER-TREE-WALK(x.right);

END OF PREORDER-TREE-WALK

 

 

POSTORDER-TREE-WALK(x):

if(x!=null):

POSTORDER-TREE-WALK(x.left);

POSTORDER-TREE-WALK(x.rigth);

output x.key;

END OF POSTORDER-TREE-WALK

 

 

12.5:

 

Since after building this BST, one would automatically give the elements an order which

is the same as sorting. Thus it would take O(n lg n) time

 

 

END OF CHAPTER 12.1