@@ -54,7 +54,10 @@ public TreeNode<T> Parent
5454 }
5555 }
5656
57- // jk - Collated parentage in a form that is serializable without recursion problems
57+
58+ public string ParentKey { get ; set ; }
59+
60+ // jk - Collated parentage in a form that is serializable without recursion errors
5861 public List < T > ParentValueChain { get ; set ; } = new List < T > ( ) ;
5962
6063 public T Value { get { return _value ; } }
@@ -66,25 +69,55 @@ public ReadOnlyCollection<TreeNode<T>> Children
6669
6770 public TreeNode < T > AddChild ( T value )
6871 {
69- var node = new TreeNode < T > ( value ) ;
72+ var node = new TreeNode < T > ( value ) ;
73+
74+ // set appropriate parentage on the new node.
75+ // As my child - you get all of my own parentage, plus me...
7076 node . ParentValueChain . AddRange ( this . ParentValueChain ) ;
7177 node . ParentValueChain . Add ( this . Value ) ;
78+
79+ // this no longer gets cached - left here for legacy
7280 node . Parent = this ;
7381
82+ // node.ParentKey = (this.Value as NavigationNode).Key;
83+
7484 _children . Add ( node ) ;
7585 return node ;
7686 }
7787
7888 public TreeNode < T > AddChild ( TreeNode < T > node )
7989 {
80- node . ParentValueChain . AddRange ( this . ParentValueChain ) ;
90+ // When adding a pre-existing child node to the tree,
91+ // the receiving parent node takes control of re-asserting the
92+ // parentage lineage onto the newly added node plus all of its children
93+
94+ // As my child - you get all of my own parentage, plus me...
95+ node . ParentValueChain = new List < T > ( this . ParentValueChain ) ;
8196 node . ParentValueChain . Add ( this . Value ) ;
97+
98+ // and likewise recursive through the children of the newly added child
99+ SetChildParentage ( node ) ;
100+
101+ // this no longer gets cached - left here for legacy
82102 node . Parent = this ;
83103
104+ // node.ParentKey = (this.Value as NavigationNode).Key;
105+
84106 _children . Add ( node ) ;
85107 return node ;
86108 }
87109
110+
111+ private void SetChildParentage ( TreeNode < T > parent )
112+ {
113+ foreach ( var ch in parent . Children )
114+ {
115+ ch . ParentValueChain = new List < T > ( parent . ParentValueChain ) ;
116+ ch . ParentValueChain . Add ( parent . Value ) ;
117+ SetChildParentage ( ch ) ;
118+ }
119+ }
120+
88121 public TreeNode < T > [ ] AddChildren ( params T [ ] values )
89122 {
90123 return values . Select ( AddChild ) . ToArray ( ) ;
@@ -130,8 +163,12 @@ public TreeNode<T> GetParent()
130163 var parentChainValue = ParentValueChain [ parentChainCount - 1 ] ;
131164 var parentNode = new TreeNode < T > ( parentChainValue ) ;
132165
133- // ordering is preserved during List manipulation
134- parentNode . ParentValueChain . AddRange ( ParentValueChain ) ;
166+ // need to re-create the upwards parentage on this new parentNode
167+ // (ordering is preserved during List manipulation)
168+
169+ // my parent has the same parent lineage as me, only without the last one
170+ // (which is the parent itself)
171+ parentNode . ParentValueChain = new List < T > ( this . ParentValueChain ) ;
135172 parentNode . ParentValueChain . RemoveAt ( parentChainCount - 1 ) ;
136173
137174 return parentNode ;
0 commit comments