Index: C:/xampp/htdocs/wordpress_trunk/wp-includes/classes.php
===================================================================
--- C:/xampp/htdocs/wordpress_trunk/wp-includes/classes.php	(revision 8889)
+++ C:/xampp/htdocs/wordpress_trunk/wp-includes/classes.php	(working copy)
@@ -409,6 +409,7 @@
 
 /*
  * A class for displaying various tree-like structures.
+ * The algorithm achieves O(n) in complexity
  * Extend the Walker class to use it, see examples at the bottom
  */
 class Walker {
@@ -438,7 +439,7 @@
 
 		$id = $element->$id_field;
 
-		// descend only the depth is right and there are chilrens for this element
+		// descend only when the depth is right and there are childrens for this element
 		if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
 
 			foreach( $children_elements[ $id ] as $child ){
@@ -543,6 +544,105 @@
 
 		 return $output;
 	}
+	
+	/**
+ 	 * paged_walk() - produce a page of nested elements
+ 	 *
+ 	 * Given an array of hierarchical elements, the maximum depth, a specific page number,
+ 	 * and number of elements per page, this function first determines all top level root elements 
+ 	 * belonging to that page, then lists them and all of their children in hierarchical order.
+ 	 * 
+ 	 * @package WordPress
+ 	 * @since 2.7
+ 	 * @param $max_depth = 0  means display all levels; $max_depth > 0  specifies the number of display levels.
+ 	 * @param $page_num the specific page number, beginning with 1. 
+ 	 * @return XHTML of the specified page of elements 
+ 	 */
+	function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
+	
+		/* sanity check */
+		if ( empty($elements) || $max_depth<0 || $page_num<1 || $per_page<0 )
+			return ''; 
+		
+		$args = array_slice( func_get_args(), 4 );
+		$output = '';
+		
+		$id_field = $this->db_fields['id'];
+		$parent_field = $this->db_fields['parent'];
+		
+		/*
+		 * seperate elements into two buckets: top level and children elements
+		 * children_elements is two dimensional array, eg.
+		 * children_elements[10][] contains all sub-elements whose parent is 10.
+		 */
+		$top_level_elements = array();
+		$children_elements  = array();
+		foreach ( $elements as $e) {
+			if ( 0 == $e->$parent_field )
+				$top_level_elements[] = $e;
+			else
+				$children_elements[ $e->$parent_field ][] = $e;
+		}
+	
+		$count = -1; 
+		$start = ( (int)$page_num - 1 ) * (int)$per_page; 
+		$end   = $start + $per_page; 
+		$total_top = count( $top_level_elements );
+
+		foreach( $top_level_elements as $e ){
+				
+			$count++; 
+			
+			//for the last page, need to unset earlier children in order to keep track of orphans
+			if ( $end >= $total_top && $count < $start ) 
+					$this->unset_children( $e, $children_elements ); 
+				
+			if ( $count < $start )
+				continue; 
+				
+			if ( $count >= $end )
+				break; 
+					
+			$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
+		}
+			
+		if ( $end >= $total_top && count( $children_elements ) > 0 ){
+			$empty_array = array();
+			foreach ( $children_elements as $orphans )
+				foreach( $orphans as $op )
+					$this->display_element( $op, $empty_array, 1, 0, $args, $output );
+		}
+		
+		return $output;
+	}
+	
+	function get_number_of_root_elements( $elements ){
+		
+		$num = 0; 
+		$parent_field = $this->db_fields['parent'];
+		
+		foreach ( $elements as $e) {
+			if ( 0 == $e->$parent_field )
+				$num++;
+		}
+		return $num; 
+	}
+	
+	// unset all the children for a given top level element
+	function unset_children( $e, &$children_elements ){
+	
+		if ( !$e || !$children_elements )
+			return; 
+			
+		$id_field = $this->db_fields['id'];
+		$id = $e->$id_field;
+		
+		foreach ( (array)$children_elements[$id] as $child )
+			$this->unset_children( $child, $children_elements ); 
+			
+		unset( $children_elements[$id] ); 
+
+	}
 }
 
 class Walker_Page extends Walker {

