Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary tree for words and simbols
Essence of the question
construct a binary tree in which the cells would represent 1 letter from the word. but only with one slightly illogical detail.

Here is an example of a tree that should be made up: https://i.imgur.com/DVzakd3.png

in the beginning there was added 'test', then 'term'. and as you can see, r should go not after e, but after s. if now add the word 'tell' there - the letter l will go to the rith-child from r.

here's my code: https://hastebin.com/sohozozaho.py

the problem is immediately noticeable by the fact that when the tree is displayed, 1 symbol is displayed. that is, the tree does not want to write anywhere except at the root. I tried to use debug in PyCharm - I still do not understand where I made a mistake.
sparkz_alot wrote Mar-13-2018, 01:32 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Do not post images of code or links to code.
Okay, I'll try again. I did smething, and now it makes tree, but not correct.

here is all code. just in case
class Node:
    def __init__(self, value=None, left_child=None, right_child=None):
        self.value = value
        self.left_child = left_child
        self.right_child = right_child
        self.isword = ''

class BinaryTree:
    def __init__(self):
        self.root = None

    def insert(self, word):
        char_list = list(word)
        if self.root is None:
            self.root = Node(char_list[0])
            del char_list[0]
        end_node = self._insert(char_list, self.root)
        end_node = word

    def _insert(self, char_list, cur_node):
        for char in char_list:
            if cur_node.value is None:
                cur_node.value = char
                del char_list[0]
                if len(char_list) == 0:
                    return cur_node.isword
                cur_node.left_child = Node()
                return self._insert(char_list, cur_node.left_child)
            elif char == cur_node.value:
                del char_list[0]
                if len(char_list) == 0:
                    return cur_node.isword
                cur_node.left_child = Node()
                return self._insert(char_list, cur_node.left_child)
            # elif char != cur_node:
                cur_node.right_child = Node()
                return self._insert(char_list, cur_node.right_child)

    def search(self, word):
        if self.root is None:
            return False
            return self._search(word, cur_node=self.root)

    def _search(self, word, cur_node, char_list=None):
        if char_list is None:
            char_list = list(word)
        elif len(char_list) == 0:
            return False

        if cur_node.value is None:
            return False
        elif cur_node.value != char_list[0]:
            self._search(word, cur_node.right_child, char_list)
        elif cur_node.value == char_list[0]:
            if cur_node.isword == word:
                return True
            del char_list[0]
            self._search(word, cur_node.left_child, char_list)

    def print_tree(self):
        if self.root is None:
            print('Tree is empty!')

    def _print_tree(self, cur_node):
        if cur_node is None:
            # print('Node is empty')

tree = BinaryTree()
# tree.insert('tell')

# print(tree.search('test'))

where my mistake?
If interested, There are some really good btree examples in python with diagrams here: http://www.laurentluce.com/posts/binary-...in-python/
Well, I know what is binary tree, I can make it with numbers easyly. but this one - with simbols.
so... I'm sory, but could someone say me where exactly I am wrong?

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  capitalizing words in list Truman 1 85 Feb-19-2018, 11:40 PM
Last Post: Larz60+
  Program: Words after "G"/"g" Truman 3 193 Feb-15-2018, 10:37 PM
Last Post: metulburr
  read a binary file to find its type atux_null 7 404 Nov-24-2017, 10:18 AM
Last Post: DeaD_EyE
  Python, breaking words Folija 4 248 Nov-18-2017, 10:04 PM
Last Post: Folija
  Incrementing binary number Danielk121 2 247 Nov-10-2017, 12:04 PM
Last Post: Danielk121
  Efficiently find words sumithar 5 981 Feb-08-2017, 01:29 AM
Last Post: sumithar

Forum Jump:

Users browsing this thread: 1 Guest(s)