Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is my Tree code too long?
#1
So I'm building my foundation for strong AI (a data compressor/predictor). You can see my code below (output is below it). It builds a tree made of 2 4-letter branches in this example (abab and baba). You can see at the start ab and 3 8, if you chose b then you take the pointer/number '8' and go to 8th item in list, that's what the code does. I'm happy with it but is the code too long or actually very short? It's my first algorithm and any tree I've seen on the internet was complex code (others said trees were complex/scary), but mine seems simple, I think.

tree = ['']
window_start = 1
window_end = 4
for count2 in range(2):
  window = 'ababa'[window_start - 1 : window_end]
  window_start = window_start + 1
  window_end = window_end + 1
  char_location = 1
  node = 1
  for count in range(4):
    char_in_window = window[char_location - 1]
    char_location = char_location + 1
    char_index = tree[node - 1].find(char_in_window) + 1
    if char_index == 0:
      tree[node - 1] = str(tree[node - 1]) + str(char_in_window)
      if char_location != 5:
        if node == len(tree):
          tree.append([])
        tree[(node + 1 - 1)].append(len(tree) + 1)
        tree.append('')
      node = len(tree)
    else:
      if char_location != 5:
        goto = node + 1
      else:
        goto = node
      node = tree[goto - 1][char_index - 1]
print(tree)
['ab', [3, 8], 'b', [5], 'a', [7], 'b', 'a', [10], 'b', [12], 'a']
Reply
#2
That doesn't strike me as a tree at all. You stated that selecting "b" would take you to the 8th index, which is a "b", but indices 2 and 10 are also "b"; how would this access those?

Plus, there are two strings in the tree that break the str-list-str-list pattern - even if they don't have subnodes, they should still have an empty list for the sake of consistency. This is especially true is you're using indexing to access subsequent subnodes since changes to the pattern will alter the index needed to access all nodes down the line.

Tree structures are complex because there are a variety of checks and behaviors needed to gain the benefits. At a bare minimum, each node needs to be able to contain a value of its own and at least two nodes under it. Try your hand at making a node class that does just that - stores those three attributes. Then, think through how to assign a second node to that one's right or left.
Reply
#3
Nono,

['ab', [3, 8], 'b', [5], 'a', [7], 'b', 'a', [10], 'b', [12], 'a']

This tree above stores 2 branches 'abab' and 'baba'. My tree could store every 8 letters in 10MB of wikipedia text and you could find every 8 letters stored in the list. If you want to find 'baba' you always start at the very first item in the list - 'ab', you find where your first letter is (2nd position, assuming it is there), then you always go to next item '[3, 8]' and you'll pick the 2nd position number '8' because 'b' was 2nd position, and it points you to the 8th item 'a'. From there you have left to search 'a', [10], 'b', [12], 'a'].....a points you to item 10 'b', then 'a'. It doesn't matter how many 'b's are in the list :) you can only be directed to the item you can go to. I'm wondering if my code is compact or could be smaller.
Reply
#4
Also I made the code smaller, is it really small now for what it does or not at all?? Just curious.

tree = ['']
window_start = 1
window_end = 4
for count2 in range(2):
  window = 'ababa'[window_start - 1 : window_end]
  window_start = window_start + 1
  window_end = window_end + 1
  char_location = 1
  node = 1
  for count in range(4):
    char_in_window = window[char_location - 1]
    char_location = char_location + 1
    char_index = tree[node - 1].find(char_in_window) + 1
    if char_index == 0:
      tree[node - 1] = str(tree[node - 1]) + str(char_in_window)
      if node == len(tree):
        tree.append([])
      tree[(node + 1 - 1)].append(len(tree) + 1)
      tree.append('')
      node = len(tree)
    else:
      node = tree[(node + 1 - 1)][char_index - 1]
print(tree)
Reply
#5
Ok i refactored it to hell, code is super small. See below. But I still wonder am I doing it wrong? - Can the code be smaller? The code functions as desired and is fast and the memory issue is expected.

e=['']
g=4
for count2 in range(2):
  f='ababa'[count2:g]
  g=g+1
  c=1
  d=1
  for count in range(4):
    a=f[count-1]
    b=e[d-1].find(a)+1
    if b==0:
      e[d-1]=str(e[d-1])+str(a)
      if d==len(e):
        e.append([])
      e[d].append(len(e)+1)
      e.append('')
      d=len(e)
    else:
      d=e[d][b-1]
print(e)[b][/b]
Reply
#6
please indent with 4 spaces which is PEP8 compliance.
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 555 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  Does this code need to be so long? duckredbeard 4 838 Sep-27-2022, 09:36 AM
Last Post: ibreeden
  how long can a line of code be? Skaperen 2 2,172 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,258 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Mix-in class tree file not running the self test code. arjunsingh2908 3 2,922 Aug-14-2018, 05:46 PM
Last Post: arjunsingh2908
  What is wrong with code? Bin. Tree counting Peter_EU 3 3,399 Nov-08-2017, 08:41 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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