No. It's really. Many structures use this chart as a template.
If you, for example, have a base collection class
namespace MiniGraphLibrary { public class GraphCollection { public Node Root { set; get; } public Node FindChild(Node root) { throw new NotImplementedException(); } public Node InsertNode(Node root, Node nodeToBeInserted) { throw new NotImplementedException(); } } }
Then you can do the node action as follows:
namespace MiniGraphLibrary { public class Node { private string _info; private List<Node> _children = new List<Node>(); public Node(Node parent, string info) { this._info = info; this.Parent = parent; } public Node Parent { get; set; } public void AddChild(Node node) { if (!this.DoesNodeContainChild(node)) { node.Parent = this; _children.Add(node); } } public bool DoesNodeContainChild(Node child) { return _children.Contains(child); } } }
Please note that this is what I wrote in 2 minutes, and this breakdown is not very good in production, but the main thing is that you have a parent node and many children. When you add a child of a node to a given node, you make sure that it has a parent node. Here I first check if the child is allready in the list of children before joining the two. You can make some changes to the code and make sure that if the child is deleted, the parent lists to which it is already connected. I didn’t do it.
I did this to illustrate how this can be used. And it is used in many places. Fx clustered indexes in MSSQL use some kind of tree similar to a view. But I am NOT an expert on this, so correct me if I am wrong.
I have not implemented two classes in the GraphCollection class. The disadvantage of my small example is that if you intend to implement the Find method, you must go through the entire graph. You can create a binary tree in which there are only two children:
namespace MiniTreeLibrary { public class SimpleNode { private string _info; private SimpleNode _left; private SimpleNode _right; private SimpleNode _parent; public SimpleNode(Node parent, string info) { this._info = info; this.Parent = parent; } public Node Parent { get; private set; } } }
I skipped the insert on the right and left. Now with this binary tree you can do a pretty quick search if you want! But this is another discrepancy.
There are many rules when they come trees and graphs, and my graph is even a real graph. But I gave these examples so you can see that it is used a lot! If you want to move more to linear and other data structures, see the Series of articles. In parts 3, 4, and 5, they talk more about trees and graphs.