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

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 2
Reputation: jimmypk is an unknown quantity at this point 
Solved Threads: 0
jimmypk jimmypk is offline Offline
Newbie Poster

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

 
0
  #1
Jul 18th, 2006
  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, 8 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

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

 
0
  #2
Jul 18th, 2006
If you insist writing your own destructor for class Tree, change it to this:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 2
Reputation: jimmypk is an unknown quantity at this point 
Solved Threads: 0
jimmypk jimmypk is offline Offline
Newbie Poster

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

 
0
  #3
Jul 19th, 2006
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()
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

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

 
0
  #4
Jul 20th, 2006
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.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC