算法题,不用递归,构造树型

刚发了今天上午的面试题,最后一道算法题,我又看了一下,貌似真不太好实现,要在一个方法中构造整颗树,还不能使用递归.我先把自己的实现发上来,希望能起来抛砖引玉的作用.
8.算法题.
表结构:
ID        ParentID      Text
1          NULL           Root
2          1                 A1
3           1                A2
4           2                B
5           4                C
把以下程序补充完整.
public class TreeNode{.........}
public class Test
{
    public TreeNode LoadTreeNodeFromDatabase(string connectionString){.......}
    /*********************
输出结果如下:
Root
-A1
--B
---C
-A2
   **********************/
public void Print(TreeNode nod){..........}

}
我的实现如下,
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Test t 
= new Test();
            TreeNode tree 
= t.LoadTreeNodeFromDatabase("server=.\\SQLExpress;uid=sa;pwd=sa;database=ksl");
            t.Print(tree);
            Console.Read();
        }

    }

    
public class TreeNode
    
{
        
int _ID = 0;
        
int _ParentID = 0;
        
string _Text = "";
        List
<TreeNode> nodes = new List<TreeNode>();

        
public TreeNode()
        
{ }
        
public TreeNode(int id, int parentid, string text)
        
{
            _ID 
= id;
            _ParentID 
= parentid;
            _Text 
= text;
        }

        
public int ID
        
{
            
get return _ID; }
            
set { _ID = value; }
        }

        
public int ParentID
        
{
            
get return _ParentID; }
            
set { _ParentID = value; }
        }

        
public string Text
        
{
            
get return _Text; }
            
set { _Text = value; }
        }

        
public List<TreeNode> Nodes
        
{
            
get return nodes; }
        }

    }

    
public class Test
    
{
        
public TreeNode LoadTreeNodeFromDatabase(string connectionString)
        
{
            
using (SqlConnection conn = new SqlConnection(connectionString))
            
{
                SqlCommand cmd 
= new SqlCommand("SELECT ID,ParentID,Text FROM TblTree", conn);
                conn.Open();
                SqlDataReader reader 
= cmd.ExecuteReader();
                Dictionary
<string, TreeNode> dict = new Dictionary<string, TreeNode>();
                
while (reader.Read())
                
{
                    
int parentID = 0;
                    
if (reader["ParentID"!= DBNull.Value)
                        parentID 
= Convert.ToInt32(reader["ParentID"]);
                    TreeNode node 
= new TreeNode(Convert.ToInt32(reader["ID"]), parentID, reader["Text"].ToString());
                    dict.Add(node.ID.ToString(), node);
                }

                reader.Close();
                conn.Close();

                TreeNode tree;
                tree 
= dict["1"];
                
foreach (string str in dict.Keys)
                
{
                    
if (str == "1"continue;
                    TreeNode node 
= dict[str];
                    TreeNode parentNode 
= dict[node.ParentID.ToString()];
                    
if (parentNode != null)
                        parentNode.Nodes.Add(node);
                }

                
return tree;
            }

        }


        
/***************************************************************
     
         Root
         -A1
         --B
         ---C
         -A2 
          **************************************************************
*/

        
public void Print(TreeNode node)
        
{
            
if (node.ParentID != 0)
            
{
                
for (int i = 0; i < node.ParentID; i++)
                
{
                    Console.Write(
"-");
                }

            }

            Console.WriteLine(node.Text);
            
foreach (TreeNode treeNode in node.Nodes)
                Print(treeNode);
        }

    }


}

小奎的石头已经扔出来了...
posted @ 2008-05-21 15:47  bqrm_521(小奎)  阅读(1325)  评论(11编辑  收藏  举报