Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def createTree(choices,levels):
- #This is a special kind of tree. Let's say you have 4 choices
- #The first choice has 4 branches stemming from it
- #The second has 3 branches stemming from it
- #The third has 2
- #The fourth has only 1
- #This pattern repeats for every level
- choiceArray = ['r','g','b','y'];#Hardcode choices to 4 for now
- i = 0;
- nodeList = []
- tmpNodeList = []
- for choice in choiceArray:
- Node = {'index':i,'parent':0,'name':choice}
- i+= 1;
- nodeList.append(Node);
- while(len(nodeList) > 0):
- node = nodeList.pop(0)
- #Translate index to how many branches
- branches = len(choiceArray) - node['index']
- for i in range(0,branches):
- newNode = {'index':i+node['index'],'parent':node,'name':choiceArray[i+node['index']]}
- tmpNodeList.append(newNode)
- if(len(nodeList) == 0):
- for node in tmpNodeList:
- nodeList.append(node)
- tmpNodeList = [];
- levels -= 1;
- if(levels <= 1):
- #Done!
- print(len(nodeList))
- break
- for i in range(2,11):
- print("Num: " + str(i))
- createTree(['r','g','b','y'],i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement