Java 随机生成由对象及其父对象组成的树结构
我正在尝试随机生成一个对象的树结构,其外观如下所示:Java 随机生成由对象及其父对象组成的树结构,java,random,tree,Java,Random,Tree,我正在尝试随机生成一个对象的树结构,其外观如下所示: Branch parent = new Branch("Start"); Branch branch1 = new Branch(parent, "Branch 1"); Branch branch2 = new Branch(parent, "Branch 2"); Branch branch21 = new Branch(branch2, "Branch 2.1"); Branch branch22 = new Branch(branch
Branch parent = new Branch("Start");
Branch branch1 = new Branch(parent, "Branch 1");
Branch branch2 = new Branch(parent, "Branch 2");
Branch branch21 = new Branch(branch2, "Branch 2.1");
Branch branch22 = new Branch(branch2, "Branch 2.2");
我知道如何手动创建对象,以及如何生成随机数,我还看到了一些关于随机生成节点以创建分形的内容,但我突然迷路了,因为我以前从未以编程方式生成对象
任何关于从何处开始或使用何种技术的想法都将不胜感激。我经常这样做,我肯定能在某些效率方面提供帮助 尝试以下类似的逻辑。在控制台上放置断点。写入以查看深度。这是一个C++面向对象的方法,我个人更喜欢LINQ来实现性能。我没有对这个进行微调,所以LINQ可能会更快。但是,如果您知道自己在做什么,就可以通过自定义方法和继承显著提高性能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication11
{
public class Branch
{
//Branch Name
public string BranchName { get; set; }
//Add depth
public List<Branch> Children = new List<Branch>();
//Add a parent
public Branch Parent = null;
public Branch()
{
}
public Branch(string BranchName)
{
this.BranchName = BranchName;
}
public Branch(Branch Parent, string BranchName)
{
this.Parent = Parent;
this.BranchName = BranchName;
}
//Internal Functions
public bool HasParent()
{
return this.Parent != null;
}
public Branch getParent()
{
return this.Parent;
}
}
//Wrap a list inside of a class so we can have leverage over the add, and create our own functions for our need
public class BranchList
{
public List<Branch> branchParentList = new List<Branch>();
public void AddBranchToParent(Branch parent, Branch child)
{
foreach (Branch node in branchParentList)
{
if (node == parent)
{
node.Children.Add(child);
}
}
}
public void AddBranchAsChild(string parent, Branch child)
{
foreach (Branch node in branchParentList)
{
if (node.BranchName == parent)
{
node.Children.Add(child);
return;//Exit out, don't do the other loop. We found it
}
//Depth
AddBranchAsChildInChildren(node, child,parent);
}
}
public void AddBranchAsChildInChildren(Branch branch,Branch Child,string parent)
{
foreach(Branch child in branch.Children)
{
if (child.BranchName == parent)
child.Children.Add(Child);
}
}
public void AddBranchAsChildInChildren(Branch branch, string Child, string parent)
{
foreach (Branch child in branch.Children)
{
if (child.BranchName == parent)
child.Children.Add(new Branch() { BranchName=Child });
}
}
public void AddBranchAsChild(string parent, string child)
{
foreach (Branch node in branchParentList)
{
if (node.BranchName == parent)
{
node.Children.Add(new Branch() { BranchName = child });
return;
}
//Depth
AddBranchAsChildInChildren(node, child, parent);
}
}
public void AddBranchAsParent(Branch Branch, Branch Child)
{
if (branchParentList.Contains(Branch) == false)
throw new Exception("Parent exists");
foreach (Branch b in branchParentList)
{
if (b == Child)
{
b.Parent = Branch;
}
}
}
public void AddParent(Branch Parent)
{
if (branchParentList.Contains(Parent))
throw new Exception("Parent exists");
this.branchParentList.Add(Parent);
}
}
//Wrap the list, use it as an interface
public class BranchManager
{
public BranchList branchList = new BranchList();
public BranchManager()
{
}
public void AddParent(Branch Branch)
{
branchList.AddParent(Branch);
}
public void AddBranchAsChild(Branch Parent, Branch Child)
{
branchList.AddBranchToParent(Parent, Child);
}
public void AddBranchAsChild(string ParentName, Branch Child)
{
branchList.AddBranchAsChild(ParentName, Child);
}
public void AddBranchAsChild(string ParentName, string ChildName)
{
branchList.AddBranchAsChild(ParentName, ChildName);
}
}
class Program
{
static void Main(string[] args)
{
/*
Branch parent = new Branch("Start");
Branch branch1 = new Branch(parent, "Branch 1");
Branch branch2 = new Branch(parent, "Branch 2");
Branch branch21 = new Branch(branch2, "Branch 2.1");
Branch branch22 = new Branch(branch2, "Branch 2.2");
*/
BranchManager branchManager = new BranchManager();
branchManager.AddParent(new Branch("Start"));
branchManager.AddBranchAsChild("Start", "Branch 1");
branchManager.AddBranchAsChild("Start", "Branch 2");
branchManager.AddBranchAsChild("Branch 2", "Branch 2.1");
branchManager.AddBranchAsChild("Branch 2", "Branch 2.2");
Console.WriteLine(branchManager.branchList.branchParentList.Count);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序11
{
公营部门
{
//分支机构名称
公共字符串BranchName{get;set;}
//增加深度
public List Children=new List();
//添加父项
公共分支父级=null;
公共部门()
{
}
公共分支(字符串分支名称)
{
this.BranchName=BranchName;
}
公共分支(分支父级、字符串分支名称)
{
这个。父=父;
this.BranchName=BranchName;
}
//内部功能
公共布尔HasParent()
{
返回此。父项!=null;
}
公共分支getParent()
{
返回此项。父项;
}
}
//在类中包装一个列表,这样我们就可以利用add,并根据需要创建自己的函数
公共类分支列表
{
public List branchParentList=新列表();
public void AddBranchToParent(分支父级、分支子级)
{
foreach(branchParentList中的分支节点)
{
如果(节点==父节点)
{
node.Children.Add(子节点);
}
}
}
public void addBranchAshild(字符串父级,分支子级)
{
foreach(branchParentList中的分支节点)
{
if(node.BranchName==父节点)
{
node.Children.Add(子节点);
return;//退出,不要执行另一个循环。我们找到了它
}
//深度
AddBranchAsChildInChildren(节点、子节点、父节点);
}
}
public void AddBranchAsChildInChildren(分支分支、分支子级、字符串父级)
{
foreach(Branch.Children中的分支子级)
{
if(child.BranchName==父级)
child.Children.Add(child);
}
}
public void AddBranchAsChildInChildren(分支分支、字符串子级、字符串父级)
{
foreach(Branch.Children中的分支子级)
{
if(child.BranchName==父级)
Add(新分支(){BranchName=child});
}
}
public void addBranchAshild(字符串父级,字符串子级)
{
foreach(branchParentList中的分支节点)
{
if(node.BranchName==父节点)
{
添加(新分支(){BranchName=child});
返回;
}
//深度
AddBranchAsChildInChildren(节点、子节点、父节点);
}
}
public void AddBranchAsParent(分支、分支子级)
{
if(branchParentList.Contains(Branch)==false)
抛出新异常(“父级存在”);
foreach(branchParentList中的分支b)
{
if(b==Child)
{
b、 父=分支;
}
}
}
公共void AddParent(分支父级)
{
if(branchParentList.Contains(父级))
抛出新异常(“父级存在”);
this.branchParentList.Add(父级);
}
}
//包装列表,将其用作接口
公共类分支管理器
{
public BranchList BranchList=新建BranchList();
公共分行经理()
{
}
公共无效添加父级(分支)
{
branchList.AddParent(分支);
}
public void addBranchAshild(分支父级、分支子级)
{
branchList.AddBranchToParent(父,子);
}
public void addBranchAshild(字符串ParentName,分支子级)
{
branchList.AddBranchAsChild(父项名称,子项);
}
public void addBranchAshild(字符串ParentName,字符串ChildName)
{
branchList.AddBranchAsChild(父名、子名);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
/*
分支母公司=新分支(“开始”);
Branch branch1=新分支(父级,“分支1”);
Branch branch2=新分支(父级,“分支2”);
分支机构branch21=新分支机构(branch2,“分支机构2.1”);
Branch branch22=新分支(branch2,“分支2.2”);
*/
BranchManager BranchManager=新BranchManager();
branchManager.AddParent(新分支(“开始”);
branchManager.AddBranchAsChild(“开始”、“分支1”);
branchManager.AddBranchAsChild(“开始”、“分支2”);
branchManager.AddBranchAsChild(“分支2”、“分支2.1”);
branchManager.AddBranchAsChild(“Bran
Branch start = new Branch();
generateChildren(start);
private void generateChildren(Branch parent) {
int numberOfBranches = (int) (Math.random() * 5);
for (int i = 0; i < numberOfBranches; i++) {
Branch child = new Branch(parent);
generateChildren(child);
}
}
Branch start = new Branch();
CreateChildren(start);
const int ChildrenLimitCheck = 0;
private void CreateChildren(Branch parent) {
//Use a limit variable so that you can decrease, and if it's equal to a sepcific number(usually 0) exit.
int Limit = (int) (Math.random() * 5);
//Call the function that's recursive, inside of a function that isn't recursive. This gives you a clean way to interface with the recursive function without excessive lines of code in other areas
generateChildren(parent,Limit);
}
private void generateChildren(Branch parent,int limit) {
//Check to see if we've hit our minimum. If so, exit out of the logic
if(limit == ChildrenLimitCheck)
return;
//Specify the random number of branches created in this instance
int numberOfBranches = (int) (Math.random() * 5);
for (int i = 0; i < numberOfBranches; i++) {
Branch child = new Branch(parent);
parent.Children.Add(child);
//Depending on what you want to do, either pass i or pass limit. If you don't use limit, you can remove it from this function :)
//If you pass i, try doing:
//for (int i = numberOfBranches; i > 0; i--)
//So that you can eventually get down to 0, to automatically stop your recursive calls with the above return statement.
//Seems you just want to run a loop for xxx number of times. This will still grant that, but you won't have to guess the upper limit
//of numberOfBranches to exit on, and you'll be able to exit cleanly
//This may be what caused your stackoverflow error. For all recursive functions, you need an exit condition or it will run indefinately
generateChildren(child,--limit);
//generateChildren(child,i);
}
}