Allan, Once again, thanks for the help. I need to read more on OOPS it is all new and that was my first attempt. I looked at nesting Dictionaries and it got quite complex fast with the length of the strings and the path as you have outlined. I have parked that structure style for now.
All these answers are really helping me to improve my programming knowledge and narrowing my definition. The reason why I am using the tree structure like a file system. Is I am going to attempt to write some code to walk down the tree based upon certain conditions which I am still working on. Based upon the test conditions will determine how I move up and down the tree. The high picture is I am trying to build a dynamic search functionality based upon keywords in the text. Like a store name or organisation name. The text has been cleaned up before applying it to the tree based upon text or punctuation and digits that are not required. Such as receipt numbers, postcodes, braces, etc. This is mostly done. I am hoping the tree structure will help in finishing the search capability. I will only know once I have tried it. If not, then I will look for another method. I am enjoying what I am doing because I am learning. I am pushing myself beyond my normal level of skills. Playing in the dark as I say. LOL Sean -----Original Message----- From: Tutor <tutor-bounces+mhysnm1964=gmail....@python.org> On Behalf Of Alan Gauld via Tutor Sent: Wednesday, 26 June 2019 2:48 AM To: tutor@python.org Subject: Re: [Tutor] tree or link-list questions. On 25/06/2019 13:24, mhysnm1...@gmail.com wrote: ... the tree I am trying to create is: > > * A single root node > * A node can only have a single parent. > * A parent can have multiple children. > * Each node is unique. A tree with multiple children (where multiple is more than 2) is entirely possible but does make the code more complex. Have you considered a binary tree where items go either left or right depending on whether they are "greater than" or "less than" (for some arbitrary definition of greater and less) than the parent node? This is much simpler to code, and you will find lots of examples online. ------ monospace font needed ------- <pre> Root the brown fox cow car yellow car green house quick brown fox yellow flowers </pre> ------ end of monospace font ------- The immediate questions you must answer (because only you understand the requirements) are: 1) How do you know which tree branch to navigate down? For example you have two car nodes. If searching for car which is the correct answer? Similarly for fox? 2) How do you know to go down an extra level. For example there are 4 nodes below 'the' What determines whether a new node sits alongside the existing nodes or at a level below. 3) Having decided you need to go a level lower how do you know which subnode to go down? How do you insert the node car into the correct subnode of the?(either brown or yellow) It looks like you are doing this based on the textual context. That means you insertion code has to be stateful since it must remember the previous word inserted (or have access to the original data). That gets complicated really quickly. Similarly, when you try to retrieve the data how do yu know which node to fetch. For example if I search for fox? Surely you will need to provide the full 'path' to fox topick the right one? But if you know the path why search a tree? In that case nested dictionaries would seem a better solution? Something like(missing all the quotes!): data = { the: [ {brown: [ {fox:[]}, {cow:[]}, {car:[]}, ]}, {yellow: [ {car: []} ]}, {green: [ {house: []} ]}, {quick: [ {brown: [ {fox: []} ]}, ]}, ]}, {yellow: [ {flowers: []} ]} ]} Then you can access the appropriate path: myFox = data[the][quick][brown][fox] Just a thought. Since I don't really know how you intend to use this I'm not sure if that would work for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor