Python Forum
How to print out the children index of the search tree?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print out the children index of the search tree?
#1
Hi, I am working on alphabeta pruning and I am trying to print out the index of the pruned nodes. When I used this input 2 4 13 11 1 3 3 7 3 3 I got print out 3 6 7 5. However it is supposed to be 3 6 7 10 11. 5 is the parent's index of 10 and 11. My codes stuck at level 2 of node and it did not go all the way to children node for level 3. I wonder how do I change it from parent node in level 2 to children node in level 3? Any advice is appreciated. Thanks
def alphaBetaPrune(game_state, ALPHA, BETA):
    if game_state.depth == 4:
        return game_state.value
    if game_state.Node_type == 'MAX':
        i = 0
        # for each each_game of node do
        for each_game in game_state.leafNode:
            # calling the alphaBetaPrune recursively until it reach the DEPTH
            ALPHA = max(ALPHA, alphaBetaPrune(each_game, ALPHA, BETA))
            if BETA <= ALPHA:
                if (i + 1) < 2:
                    SharedQueue = SimpleQueue()
                    SharedQueue.put(each_game.head.leafNode[i + 1])
                    while not SharedQueue.empty():
                        curNode = SharedQueue.get_nowait()
                        if len(curNode.leafNode) == 0:
                            print(curNode.index, "", end='')
                        else:
                            for child in curNode.leafNode:
                                SharedQueue.put(child)
            i += 1
        return ALPHA
    else:
        i = 0
        # Repeat for each each_game
        for each_game in game_state.leafNode:
            BETA = min(BETA, alphaBetaPrune(each_game, ALPHA, BETA))
            if BETA <= ALPHA:
                if (i + 1) < 2:
                    SharedQueue = SimpleQueue()
                    SharedQueue.put(each_game.head.leafNode[i + 1])
                    while not SharedQueue.empty():
                        curNode = SharedQueue.get_nowait()
                        if len(curNode.leafNode) == 0:
                            print(curNode.index, "", end='')
                        else:
                            for child in curNode.leafNode:
                                SharedQueue.put(child)
                break
            i += 1
        return BETA


def somefunction():
    m_input = input().split()
    input_list = []
    for num in m_input:
        input_list.append(int(num))
    root = Node()
    new = root.leafNode
    level2_1 = Node(root)
    new.append(level2_1)
    level2_2 = Node(root)
    new.append(level2_2)
    level2_3 = Node(root)
    new.append(level2_3)
    tree_struct = []
    tree_struct.extend([level2_1, level2_2, level2_3])
    SharedQueue = SimpleQueue()
    for element in tree_struct:
        SharedQueue.put(element)
    while len(input_list) > 0:
        curNode = SharedQueue.get()
        if curNode.leafNode is not None:
            level = 4
            while level > 2:
                leafNode = Node(curNode)
                if leafNode.depth == 4:
                    leafNode.value = input_list.pop(0)
                curNode.leafNode.append(leafNode)
                SharedQueue.put(leafNode)
                level = level - 1

    return root
#2
You have not supplied enough code. I don't know what Node is or have any idea how depth or index are assigned.
#3
Hi, here it is

DEPTH = 4
MAX = 1000
MIN = -1000


class Node:
    def __init__(self, head=None):
        self.head = head
        self.leafNode = []
        self.value = MIN
        self.Node_type = 'MAX'
        self.index = 0
        self.depth = 1
        if self.head == None:
            return
        elif self.head.Node_type == 'MAX':
            self.Node_type = 'MIN'
            self.depth = self.head.depth + 1
            self.index = len(self.head.leafNode) + self.head.index * 2
            self.value = MAX
        elif self.head.Node_type == 'MIN':
            self.depth = self.head.depth + 1
            self.index = len(self.head.leafNode) + self.head.index * 2
#4
And how do you call alphaBetaPrune?
#5
(Apr-01-2022, 08:29 PM)deanhystad Wrote: And how do you call alphaBetaPrune?

alphaBetaPrune(moodle_input(), MIN, MAX)

Then I supplied the input.
#6
I cannot duplicate your results. I thought it was because I called the pruning function incorrectly, but now I think it is because I am starting with a different tree. I was using the tree returned by somefunction() in your original post.
longmen likes this post
#7
(Apr-01-2022, 11:00 PM)deanhystad Wrote: I cannot duplicate your results. I thought it was because I called the pruning function incorrectly, but now I think it is because I am starting with a different tree. I was using the tree returned by somefunction() in your original post.

Thank you for trying.
#8
Some of the posts by @longmen have been reverted back to the originals as they were all changed to
Quote:Please disregard this
I received a PM regarding deleting this please read the following link
Editing or deleting threads/posts/accounts
This thread has been locked.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Python Inheritance (One parent, two children) b_salm 3 1,920 Jan-29-2020, 08:09 AM
Last Post: buran
  Print Binary Tree - Python Daniel94 1 2,659 Jan-08-2020, 09:54 PM
Last Post: ichabod801
  Sorting Santa's List of Children sirox84 4 5,173 Feb-20-2017, 06:10 PM
Last Post: sirox84
  Print the index of the vowels in a string MeeranRizvi 4 14,689 Dec-29-2016, 02:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020