File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -80,6 +80,8 @@ DIAG_DIR = diag/
8080CLA_DIR = $(DIAG_DIR ) cla/
8181# The folder where the flowcharts diagrams are located
8282FLO_DIR = $(DIAG_DIR ) flo/
83+ # The folder where the graph diagrams are located
84+ GRA_DIR = $(DIAG_DIR ) gra/
8385# The folder where the videos are located.
8486VID_DIR = vid/
8587# The folders where the project are located.
@@ -374,6 +376,25 @@ $(FLO_DIR)%.png: $(FLO_DIR)%.txt
374376 && mmdc -s 2 -p .puppeteer.json -i $@ _tmp -o $@ \
375377 && rm $@ _tmp
376378
379+ # Graph diagram are only used for
380+ # binary trees.
381+ $(GRA_DIR ) % .md : $(GRA_DIR ) % .txt | $(GRA_DIR ) % .svg $(GRA_DIR ) % .png
382+ echo "  .txt), [image version](./$( basename $@ ) .png), [svg version](./$( basename $@ ) .svg))]($( notdir $( basename $@ ) ) .png)" > $@
383+
384+ $(GRA_DIR ) % .svg : $(GRA_DIR ) % .txt
385+ @echo " Creating $@ "
386+ echo " graph TB" > $@ _tmp \
387+ && cat $< >> $@ _tmp \
388+ && mmdc -p .puppeteer.json -i $@ _tmp -o $@ \
389+ && rm $@ _tmp
390+
391+ $(GRA_DIR ) % .png : $(GRA_DIR ) % .txt
392+ @echo " Creating $@ "
393+ echo " graph TB" > $@ _tmp \
394+ && cat $< >> $@ _tmp \
395+ && mmdc -s 2 -p .puppeteer.json -i $@ _tmp -o $@ \
396+ && rm $@ _tmp
397+
377398# Exercise without solution
378399$(EXO_DIR ) % .md : $(SOL_DIR ) % .md
379400 @echo " Creating $@ "
Original file line number Diff line number Diff line change @@ -25,7 +25,16 @@ public bool IsEmpty()
2525
2626 public void Clear ( )
2727 {
28- alist = new T [ 0 ] ;
28+ if ( isReadonly )
29+ {
30+ throw new InvalidOperationException (
31+ "List is read-only, you cannot clear it."
32+ ) ;
33+ }
34+ else
35+ {
36+ alist = new T [ 0 ] ;
37+ }
2938 }
3039
3140 // Attribute and property for the
Original file line number Diff line number Diff line change @@ -122,7 +122,16 @@ static void Main(string[] args)
122122 {
123123 Console . WriteLine ( ex . Message ) ;
124124 }
125+ try
126+ {
127+ myList1 . Clear ( ) ;
128+ }
129+ catch ( Exception ex )
130+ {
131+ Console . WriteLine ( ex . Message ) ;
132+ }
125133
134+ /* Testing copying into an array.*/
126135 int [ ] array = new int [ myList1 . Count + 1 ] ;
127136 try
128137 {
Original file line number Diff line number Diff line change @@ -137,4 +137,77 @@ private bool Find(Node nodeP, T dataP)
137137
138138 public abstract void Insert ( T dataP ) ;
139139 public abstract bool Delete ( T dataP ) ;
140+
141+ /* Traversal methods. */
142+ // Inorder traversal
143+ // "Left, root, right"
144+ public string TrasverseI ( )
145+ {
146+ string returned = "" ;
147+ if ( root != null )
148+ {
149+ returned += TraverseI ( root ) ;
150+ }
151+ return returned ;
152+ }
153+
154+ private string TraverseI ( Node nodeP )
155+ {
156+ string returned = "" ;
157+ if ( nodeP != null )
158+ {
159+ returned += TraverseI ( nodeP . left ) ;
160+ returned += nodeP . Data + " " ;
161+ returned += TraverseI ( nodeP . right ) ;
162+ }
163+ return returned ;
164+ }
165+
166+ // Preorder traversal
167+ // "Root, left, right"
168+ public string TrasversePr ( )
169+ {
170+ string returned = "" ;
171+ if ( root != null )
172+ {
173+ returned += TraversePr ( root ) ;
174+ }
175+ return returned ;
176+ }
177+
178+ private string TraversePr ( Node nodeP )
179+ {
180+ string returned = "" ;
181+ if ( nodeP != null )
182+ {
183+ returned += nodeP . Data + " " ;
184+ returned += TraversePr ( nodeP . left ) ;
185+ returned += TraversePr ( nodeP . right ) ;
186+ }
187+ return returned ;
188+ }
189+
190+ // Postorder traversal
191+ // "Left, right, root"
192+ public string TrasversePo ( )
193+ {
194+ string returned = "" ;
195+ if ( root != null )
196+ {
197+ returned += TraversePo ( root ) ;
198+ }
199+ return returned ;
200+ }
201+
202+ private string TraversePo ( Node nodeP )
203+ {
204+ string returned = "" ;
205+ if ( nodeP != null )
206+ {
207+ returned += TraversePo ( nodeP . left ) ;
208+ returned += TraversePo ( nodeP . right ) ;
209+ returned += nodeP . Data + " " ;
210+ }
211+ return returned ;
212+ }
140213}
Original file line number Diff line number Diff line change @@ -18,6 +18,13 @@ static void Main()
1818 btree . Insert ( 15 ) ;
1919
2020 Console . WriteLine ( btree ) ;
21+ Console . WriteLine ( "Inorder traversal:" ) ;
22+ Console . WriteLine ( btree . TrasverseI ( ) ) ;
23+ Console . WriteLine ( "Preorder traversal:" ) ;
24+ Console . WriteLine ( btree . TrasversePr ( ) ) ;
25+ Console . WriteLine ( "Postorder traversal:" ) ;
26+ Console . WriteLine ( btree . TrasversePo ( ) ) ;
27+
2128 Console . WriteLine ( "Smallest value: " + btree . FindMin ( ) ) ;
2229
2330 btree . Delete ( btree . FindMin ( ) ) ;
@@ -41,5 +48,23 @@ static void Main()
4148 {
4249 Console . WriteLine ( i + " occurs: " + btree . Find ( i ) ) ;
4350 }
51+
52+ /*
53+ * Second example, we begin by clearing the tree.
54+ * The purpose of this example is to illustrate that
55+ * the order in which values are inserted matter.
56+ */
57+ btree . Clear ( ) ;
58+ btree . Insert ( 10 ) ;
59+ btree . Insert ( 6 ) ;
60+ btree . Insert ( 12 ) ;
61+ btree . Insert ( 15 ) ;
62+ Console . WriteLine ( btree ) ;
63+ btree . Clear ( ) ;
64+ btree . Insert ( 6 ) ;
65+ btree . Insert ( 12 ) ;
66+ btree . Insert ( 15 ) ;
67+ btree . Insert ( 10 ) ;
68+ Console . WriteLine ( btree ) ;
4469 }
4570}
You can’t perform that action at this time.
0 commit comments