Last active
May 10, 2023 18:01
-
-
Save sonnguyen9800/32a232d267da40587a02af75c53e3814 to your computer and use it in GitHub Desktop.
Custom Tree Traversal, C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApp1 | |
{ | |
public class FrameContainer | |
{ | |
public Frame FirstFrame; | |
public FrameContainer(Frame firstFrame) | |
{ | |
FirstFrame = firstFrame; | |
} | |
public static Frame GetFrameByValue(int frameDataValue, Frame parentFrame) | |
{ | |
if (parentFrame == null) return null; | |
var nextFrameType = parentFrame.GetNextFrameType; | |
Frame foundFrame = null; | |
if (parentFrame.DataValue == frameDataValue) return parentFrame; | |
switch (nextFrameType) | |
{ | |
case Frame.NextFrameType.None: | |
foundFrame = null; | |
break; | |
case Frame.NextFrameType.Single: | |
foundFrame = GetFrameByValue(frameDataValue, parentFrame.NextFrame); | |
break; | |
case Frame.NextFrameType.Options: | |
foreach (var optionFrame in parentFrame.OptionsFrame) | |
{ | |
var result = GetFrameByValue(frameDataValue, optionFrame); | |
if (result != null) | |
{ | |
foundFrame = result; | |
break; | |
} | |
} | |
break; | |
} | |
return foundFrame; | |
} | |
} | |
public class Frame | |
{ | |
public enum NextFrameType | |
{ | |
None, | |
Single, | |
Options | |
} | |
public Frame NextFrame = null; | |
public List<Frame> OptionsFrame = new List<Frame>(); | |
public int DataValue; | |
public string Name; | |
public Frame(int value, string name) | |
{ | |
Name = name; | |
DataValue = value; | |
} | |
public override string ToString() { | |
return DataValue.ToString(); | |
} | |
public NextFrameType GetNextFrameType | |
{ | |
get | |
{ | |
if (NextFrame != null) return NextFrameType.Single; | |
if (NextFrame == null && OptionsFrame.Count > 0 && OptionsFrame != null) return NextFrameType.Options; | |
return NextFrameType.None; | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello the world"); | |
Frame FirstFrame = new Frame(1, "First"); | |
FrameContainer MainAction = new FrameContainer(FirstFrame); | |
Frame frame2 = new Frame(2, "Sssecond"); | |
Frame frame3 = new Frame(3, "Third"); | |
Frame frame4 = new Frame(4, "Forth"); | |
Frame frame5 = new Frame(5, "Fifth"); | |
Frame frame6 = new Frame(6, "Sixth"); | |
Frame frame7 = new Frame(7, "Seventh"); | |
Frame frame8 = new Frame(8, "Eighth"); | |
Frame frame9 = new Frame(9, "Nineth"); | |
// First Frame | |
FirstFrame.NextFrame = frame2; | |
// 2 | |
frame2.NextFrame = frame3; | |
// 3 | |
frame3.OptionsFrame.Add(frame4); frame3.OptionsFrame.Add(frame5); | |
// 4 | |
frame4.NextFrame = frame6; | |
//5 | |
frame5.NextFrame = frame7; | |
//7 | |
frame7.OptionsFrame.Add(frame8); frame7.OptionsFrame.Add(frame9); | |
var res = FrameContainer.GetFrameByValue(2, MainAction.FirstFrame); | |
if (res != null) | |
Console.WriteLine("Frame Name: " + res.Name); | |
else | |
Console.WriteLine("No Data "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment