-
Notifications
You must be signed in to change notification settings - Fork 4
/
CodeFile1.cs
100 lines (82 loc) · 2.46 KB
/
CodeFile1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections.Generic;
using System.Collections;
using System;
class TreeNode
{
//private readonly Dictionary<string, TreeNode> _childs = new Dictionary<string, TreeNode>();
public List<TreeNode> _childs= new List<TreeNode>();
public readonly string url;
public readonly int depth;
public TreeNode Parent { get; private set; }
public TreeNode(string url, int curdepth=0)
{
this.url = url;
this.depth = curdepth;
}
//public TreeNode GetChild(string id)
//{
// return this._childs[id];
//}
public void Add(string url)
{
foreach (TreeNode _child in this._childs)
{
if (_child.url.ToLower() == url.ToLower())
return;
}
TreeNode item= new TreeNode(url, this.depth+1);
item.Parent = this;
this._childs.Add(item);
}
//public IEnumerator<TreeNode> GetEnumerator()
//{
// return this._childs.Values.GetEnumerator();
//}
//IEnumerator IEnumerable.GetEnumerator()
//{
// return this.GetEnumerator();
//}
public int Count
{
get { return this._childs.Count; }
}
public bool AddtoTree(string matchingUrl, string childurl)
{
var queue = new Queue<TreeNode>();
queue.Enqueue(this);
while (queue.Count > 0)
{
// Take the next node from the front of the queue
var node = queue.Dequeue();
// Process the node 'node'
if (node.url.ToLower() == matchingUrl.ToLower())
{
node.Add(childurl);
return true;
}
// Add the node’s children to the back of the queue
foreach (var child in node._childs)
queue.Enqueue(child);
}
// None of the nodes matched the specified predicate.
return false;
}
public void PrintTree()
{
var stack = new Stack<TreeNode>();
stack.Push(this);
while (stack.Count > 0)
{
// Take the next node from the front of the queue
var node = stack.Pop();
for (int index = 1; index <= node.depth; index++)
{
Console.Write("\t");
}
Console.Write(String.Format("{0}\n", node.url));
// Add the node’s children to the back of the queue
foreach (var child in node._childs)
stack.Push(child);
}
}
}