944,193 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3089
  • Python RSS
Jul 18th, 2006
0

I have a problem in building class Tree (Binary Search)

Expand Post »
Python Syntax (Toggle Plain Text)
  1. # I have a problem in building class Tree (Binary Search)
  2. # function DelNode(self, Key) is not exactly
  3. # I Need Help, Please building class Tree (Binary Search)
  4. # Is there any different definition of class Tree
  5. from string import split
  6. class Node:
  7. def __init__(self):
  8. self.Key = None
  9. self.pLeft = None
  10. self.pRight = None
  11. def __del__(self):
  12. del self
  13.  
  14. class Tree:
  15. def __init__(self):
  16. self.Root = None
  17. def __del__a(self):
  18. if self.Root != None:
  19. del self.Root.pLeft
  20. del self.Root.pRight
  21. del self.Root
  22. def LNR(self):
  23. if self.Root != None:
  24. self.Root.pLeft.LNR()
  25. print self.Root.Key,
  26. self.Root.pRight.LNR()
  27. def InsertNode(self, Key):
  28. if self.Root != None:
  29. if self.Root.Key == Key:
  30. return 0
  31. elif self.Root.Key > Key:
  32. return self.Root.pLeft.InsertNode(Key)
  33. else:
  34. return self.Root.pRight.InsertNode(Key)
  35. self.Root = Node()
  36. if self.Root == None:
  37. return -1
  38. self.Root.Key = Key
  39. self.Root.pLeft = Tree()
  40. self.Root.pRight = Tree()
  41. return 1
  42. def SearchStandFor(self, t1, t2):
  43. if t2.Root.pLeft.Root != None:
  44. self.SearchStandFor(t1, t2.Root.pLeft)
  45. else:
  46. t1.Root.Key = t2.Root.Key
  47. t1 = t2
  48. t2 = t2.Root.pRight
  49. def DelNode(self, Key):
  50. if self.Root == None:
  51. return 0
  52. if self.Root.Key > Key:
  53. return self.Root.pLeft.DelNode(Key)
  54. elif self.Root.Key < Key:
  55. return self.Root.pRight.DelNode(Key)
  56. else:# self.Root.Key == Key
  57. p = self # --> trouble ???
  58. if self.Root.pLeft.Root == None:
  59. self.Root = self.Root.pRight.Root
  60. elif self.Root.pRight.Root == None:
  61. self.Root = self.Root.pLeft.Root
  62. else:# Root has 2 children
  63. self.SearchStandFor(p, self.Root.pRight)
  64. del p # --> trouble ???
  65. return 1
  66.  
  67. def ReadFile(strFileName, t):
  68. OFile = open(strFileName, "r")
  69. text = OFile.readline()
  70. OFile.close()
  71.  
  72. a = split(text, " ")
  73. for i in range(0,len(a)):
  74. t.InsertNode(int(a[i]))
  75.  
  76. def main():
  77. print
  78. t = Tree()
  79. ReadFile("TreeIn.txt", t)
  80. t.LNR() #--> 13 16 18 31 37 40 44 55 59 71 81 108
  81. print
  82. t.DelNode(44) # 44 is root, have two children
  83. t.LNR() #--> 13 16 18 31 37 40 55 55 59 71 81 108
  84. # I write t.DelNode(13), it don't run exactly, 13 is a node have 1 child (right child)
  85. # ... and if I don't define function __del__ of clas Node, it runs exactly, why???
  86.  
  87. main()
  88. #content of file TreeIn.txt is "44 18 37 13 31 40 81 59 108 55 71 16"
Attached Files
File Type: zip CTree.zip (1.0 KB, 35 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimmypk is offline Offline
2 posts
since Mar 2006
Jul 18th, 2006
0

Re: I have a problem in building class Tree (Binary Search)

If you insist writing your own destructor for class Tree, change it to this:
Python Syntax (Toggle Plain Text)
  1. class Tree:
  2. def __init__(self):
  3. self.Root = None
  4. def __del__(self):
  5. if self.Root != None:
  6. del self.Root
  7. def LNR(self):
  8. ...
  9. ...
Note: Destructors are optional thing in Python.
Last edited by bumsfeld; Jul 18th, 2006 at 7:06 am.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 19th, 2006
0

Re: I have a problem in building class Tree (Binary Search)

def main():
    print
    t = Tree()
    ReadFile("TreeIn.txt", t)
    t.LNR() #--> 13 16 18 31 37 40 44 55 59 71 81 108
    print
    t.DelNode(44) # 44 is root, have two children
    t.LNR() #--> 13 16 18 31 37 40 55 55 59 71 81 108
    #my program doesn't del element, it only change 44 to 55, i want del node 44
main()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimmypk is offline Offline
2 posts
since Mar 2006
Jul 20th, 2006
0

Re: I have a problem in building class Tree (Binary Search)

Hi jimmypk,

It might be easier if you maintained a 'parent' field in each Node, then used this to forcibly unlink subtrees from parent Nodes. In that case, deleting a Node would be similar to deleting a Node in a linked list - set the parent's child field to the grandchild Node.

That's all the advice I have to give.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Newbie Problem
Next Thread in Python Forum Timeline: data grabbing from html sites





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC