i made a function in unity that takes a Binode - and making it into a2d tree, but here the problem: my so called idea to display the tree is this:
make a recursive function, and every time the level will go up by 1, and it will also send the function a bool(is the right or is the left of the old node) and then if its true it will get the root of the tree and - the y by level * 55, then it will either x = level * 55, if it right, and if its left the x = level * -55.
now the problome is lets say i have a tree that has 3 levels, then if i will put a right node to the left tree it will just put it were the most right node is and it will overlap, kinda hard to explain in text but i hope you understand.
heres the repository: git
code:
public void GenrateTreeUI(BinNode<int> t, bool right, int Level)
{
if (Level == 0)
{
cir = Instantiate(CirclePrefab, new Vector2(0, 0), Quaternion.identity);
t.SetGO(cir);
cir.transform.SetParent(canvas.transform);
cir.GetComponent<Image>().color = Color.black;
roottr = cir.GetComponent<RectTransform>();
Value = cir.GetComponentInChildren<TMP_Text>();
roottr.anchoredPosition = new Vector2(-11, 213);
Value.text = t.GetValue().ToString();
Value.color = Color.white;
}
else
{
if (!right)
{
cir = Instantiate(CirclePrefab, new Vector2(0, 0), Quaternion.identity);
cir.transform.SetParent(canvas.transform);
t.SetGO(cir);
cir.GetComponentInChildren<TMP_Text>().text = t.GetValue().ToString();
cir.GetComponent<RectTransform>().anchoredPosition = new Vector2(roottr.anchoredPosition.x - Level * 55,
roottr.anchoredPosition.y - (Level * 55));
}
else
{
cir = Instantiate(CirclePrefab, new Vector2(0, 0), Quaternion.identity);
cir.transform.SetParent(canvas.transform);
t.SetGO(cir);
cir.GetComponentInChildren<TMP_Text>().text = t.GetValue().ToString();
cir.GetComponent<RectTransform>().anchoredPosition = new Vector2(roottr.anchoredPosition.x + Level * 55,
roottr.anchoredPosition.y - Level * 55);
}
}
if (t.HasLeft())
{
GenrateTreeUI(t.GetLeft(), false, Level + 1);
GenrateWire(cir.GetComponent<RectTransform>().anchoredPosition, GetFutreNode(t.GetLeft(), Level, false));
}
if (t.HasRight())
{
GenrateTreeUI(t.GetRight(), true, Level + 1);
GenrateWire(cir.GetComponent<RectTransform>().anchoredPosition, GetFutreNode(t.GetRight(), Level, true));
}
}
thanks for the help!