C
C#ā€¢2y ago
Chris TCC

āœ… learning tree data structure

I've got some data that I'd like to store for a unity app. The data is perfect for a 3-level tree, but I have never used that data structure before. Currently I'm using a triple layered list, and my code is a MESS. Does anyone know any good tutorials for me to research? Currently I've tried but can only find tutorials on actual trees, and tutorials explaining how the data structure works, but not how to write the code.
36 Replies
Chris TCC
Chris TCCOPā€¢2y ago
seriously, my code is a mess. here is a snippet.
TheBoxyBear
TheBoxyBearā€¢2y ago
For a given tree, there is no set depth and different branches can end earlier or later, etc. Every implementation I've seen uses a recursive approach where each notes contains a collection of its own type storing its children
Chris TCC
Chris TCCOPā€¢2y ago
yep but how do I write a tree in code? I've seen some snippets in tutorials but I have no idea how it works or how to write the code for it
TheBoxyBear
TheBoxyBearā€¢2y ago
Can you make classes?
Chris TCC
Chris TCCOPā€¢2y ago
unity allows me to but I've never made my own class I'm 100% self taught so my coding knowledge is near 0 lol even the most basic concepts are unknown to me I tend to learn as I go
TheBoxyBear
TheBoxyBearā€¢2y ago
Haven't used Unity in ages so can't say exactly how to attach a non monobehavior class to your project It's the same syntax as a script minus the : MonoBehaviour and you don't need the Start/Update methods
Chris TCC
Chris TCCOPā€¢2y ago
I mean, I should be able to figure that out but still, how do I make a tree then? nevermind attaching it to unity, I don't even know how to write a tree
TheBoxyBear
TheBoxyBearā€¢2y ago
The class is a data type you can use to define members. So if you have a class called Node, inside it you can define a List<Note> Each instance will have its own list of itself, the branches going from that node
Chris TCC
Chris TCCOPā€¢2y ago
can each node hold multiple data types? I've got around 5 variables I'd like to store in each node
TheBoxyBear
TheBoxyBearā€¢2y ago
You can define more members to it
Chris TCC
Chris TCCOPā€¢2y ago
more members?
TheBoxyBear
TheBoxyBearā€¢2y ago
Fields properties or methods
Chris TCC
Chris TCCOPā€¢2y ago
so I can have each node in the tree store multiple data types and variables yeah?
TheBoxyBear
TheBoxyBearā€¢2y ago
Yes, classes can store as much data as you want
Chris TCC
Chris TCCOPā€¢2y ago
oh wait yeah trees are just linked classes right?
TheBoxyBear
TheBoxyBearā€¢2y ago
Each instance will have its own values for the fields and properties
Chris TCC
Chris TCCOPā€¢2y ago
is there any tutorial or example code that I can read to go through and try to figure out because I have no idea where to start when I wanna write the code for it I barely know how to write a class...
TheBoxyBear
TheBoxyBearā€¢2y ago
Tree Data Structure
In this article, you will learn about Tree Data Structure.
Chris TCC
Chris TCCOPā€¢2y ago
ah thanks I'll read through that first then legit I couldn't find any good youtube tutorials tho
TheBoxyBear
TheBoxyBearā€¢2y ago
For some reason the class is defined with the Class keyword, that should be lowercase
Chris TCC
Chris TCCOPā€¢2y ago
this?
TheBoxyBear
TheBoxyBearā€¢2y ago
yes
Chris TCC
Chris TCCOPā€¢2y ago
so it should be
public class BinaryTree{

}
public class BinaryTree{

}
?
TheBoxyBear
TheBoxyBearā€¢2y ago
šŸ‘
Chris TCC
Chris TCCOPā€¢2y ago
wtf why my formatting no work
TheBoxyBear
TheBoxyBearā€¢2y ago
$code
MODiX
MODiXā€¢2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Chris TCC
Chris TCCOPā€¢2y ago
there we go didn't know a space after ```cs caused formatting to yeet out the window šŸ˜…
TheBoxyBear
TheBoxyBearā€¢2y ago
The examples are pretty advances but the bare minimum you need is the class with a collection of its own type
Chris TCC
Chris TCCOPā€¢2y ago
as long as I can read the code and hopefully figure it out I should be able to derive my own code from that šŸ¤ž yeah no the article didn't really help... but I got this:
class Node {
public Node parent;
public List<Node> children;
public string data;

public Node(string data) {
this.data = data;
children = new List<Node>();
}

public void AddChild(Node child) {
child.parent = this;
children.Add(child);
}
}

class Tree {
public Node root;

public Tree(string data) {
root = new Node(data);
}
}

class Program {
static void Main(string[] args) {
// Create the tree
Tree tree = new Tree("Parent Node");

// Create child nodes
Node child1 = new Node("Child Node 1");
Node child2 = new Node("Child Node 2");

// Add child nodes to the root node
tree.root.AddChild(child1);
tree.root.AddChild(child2);
}
}
class Node {
public Node parent;
public List<Node> children;
public string data;

public Node(string data) {
this.data = data;
children = new List<Node>();
}

public void AddChild(Node child) {
child.parent = this;
children.Add(child);
}
}

class Tree {
public Node root;

public Tree(string data) {
root = new Node(data);
}
}

class Program {
static void Main(string[] args) {
// Create the tree
Tree tree = new Tree("Parent Node");

// Create child nodes
Node child1 = new Node("Child Node 1");
Node child2 = new Node("Child Node 2");

// Add child nodes to the root node
tree.root.AddChild(child1);
tree.root.AddChild(child2);
}
}
this was an example given to me, if this is right I'll use this as the fundamental to build upon
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Chris TCC
Chris TCCOPā€¢2y ago
No
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Chris TCC
Chris TCCOPā€¢2y ago
Nope
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Chris TCC
Chris TCCOPā€¢2y ago
Bruh Whatever
Want results from more Discord servers?
Add your server