Getting no display of the output when inserting a node in a BST
I want to insert a 'new' node in a binary search tree. But on running the code, I am either getting errors such as :
an infinte loop (with nothing as display)
temp.data(nonetype) has no value.
Can you see what's the error in this code and how to rectify the same.
Please see my following code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self):
self.root = None
def insert(self,data):
temp = root
new_node = Node(data)
new_node.left = None
new_node.right =None
while temp.left and temp.right:
if new_node.data < temp.data:
temp = temp.left
else:
temp = temp.right
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
mylist = tree()
root = Node(19)
root.left =Node(12)
root.right = Node(30)
mylist.insert(10)
mylist.display()
tree insert binary-search-tree
add a comment |
I want to insert a 'new' node in a binary search tree. But on running the code, I am either getting errors such as :
an infinte loop (with nothing as display)
temp.data(nonetype) has no value.
Can you see what's the error in this code and how to rectify the same.
Please see my following code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self):
self.root = None
def insert(self,data):
temp = root
new_node = Node(data)
new_node.left = None
new_node.right =None
while temp.left and temp.right:
if new_node.data < temp.data:
temp = temp.left
else:
temp = temp.right
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
mylist = tree()
root = Node(19)
root.left =Node(12)
root.right = Node(30)
mylist.insert(10)
mylist.display()
tree insert binary-search-tree
add a comment |
I want to insert a 'new' node in a binary search tree. But on running the code, I am either getting errors such as :
an infinte loop (with nothing as display)
temp.data(nonetype) has no value.
Can you see what's the error in this code and how to rectify the same.
Please see my following code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self):
self.root = None
def insert(self,data):
temp = root
new_node = Node(data)
new_node.left = None
new_node.right =None
while temp.left and temp.right:
if new_node.data < temp.data:
temp = temp.left
else:
temp = temp.right
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
mylist = tree()
root = Node(19)
root.left =Node(12)
root.right = Node(30)
mylist.insert(10)
mylist.display()
tree insert binary-search-tree
I want to insert a 'new' node in a binary search tree. But on running the code, I am either getting errors such as :
an infinte loop (with nothing as display)
temp.data(nonetype) has no value.
Can you see what's the error in this code and how to rectify the same.
Please see my following code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self):
self.root = None
def insert(self,data):
temp = root
new_node = Node(data)
new_node.left = None
new_node.right =None
while temp.left and temp.right:
if new_node.data < temp.data:
temp = temp.left
else:
temp = temp.right
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
mylist = tree()
root = Node(19)
root.left =Node(12)
root.right = Node(30)
mylist.insert(10)
mylist.display()
tree insert binary-search-tree
tree insert binary-search-tree
edited Jan 5 at 1:12
Drool
asked Jan 2 at 3:50
DroolDrool
214
214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your code does not take in consideration when a node has only left or right subtree. While iterating through the nodes you need to check if your left or right child is None type or not.
I have modified the tree class to initialize root in constructor.
Modified Code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self,root):
self.root = root
def insert(self,data):
temp = self.root
new_node = Node(data)
new_node.left = None #remove this as it will be set to none in Node constructor itself.
new_node.right =None #remove this
while temp.left or temp.right:
if new_node.data < temp.data:
if temp.left != None:
temp = temp.left
else:
break
else:
if temp.right !=None:
temp = temp.right
else:
break
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
root = Node(19)
#root.left =Node(12) #insert using insert function of tree class
#root.right = Node(30)
mylist = tree(root)
mylist.insert(12)
mylist.insert(30)
mylist.insert(10)
mylist.insert(20)
mylist.insert(18)
mylist.insert(9)
mylist.display(root)
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001005%2fgetting-no-display-of-the-output-when-inserting-a-node-in-a-bst%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code does not take in consideration when a node has only left or right subtree. While iterating through the nodes you need to check if your left or right child is None type or not.
I have modified the tree class to initialize root in constructor.
Modified Code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self,root):
self.root = root
def insert(self,data):
temp = self.root
new_node = Node(data)
new_node.left = None #remove this as it will be set to none in Node constructor itself.
new_node.right =None #remove this
while temp.left or temp.right:
if new_node.data < temp.data:
if temp.left != None:
temp = temp.left
else:
break
else:
if temp.right !=None:
temp = temp.right
else:
break
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
root = Node(19)
#root.left =Node(12) #insert using insert function of tree class
#root.right = Node(30)
mylist = tree(root)
mylist.insert(12)
mylist.insert(30)
mylist.insert(10)
mylist.insert(20)
mylist.insert(18)
mylist.insert(9)
mylist.display(root)
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
add a comment |
Your code does not take in consideration when a node has only left or right subtree. While iterating through the nodes you need to check if your left or right child is None type or not.
I have modified the tree class to initialize root in constructor.
Modified Code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self,root):
self.root = root
def insert(self,data):
temp = self.root
new_node = Node(data)
new_node.left = None #remove this as it will be set to none in Node constructor itself.
new_node.right =None #remove this
while temp.left or temp.right:
if new_node.data < temp.data:
if temp.left != None:
temp = temp.left
else:
break
else:
if temp.right !=None:
temp = temp.right
else:
break
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
root = Node(19)
#root.left =Node(12) #insert using insert function of tree class
#root.right = Node(30)
mylist = tree(root)
mylist.insert(12)
mylist.insert(30)
mylist.insert(10)
mylist.insert(20)
mylist.insert(18)
mylist.insert(9)
mylist.display(root)
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
add a comment |
Your code does not take in consideration when a node has only left or right subtree. While iterating through the nodes you need to check if your left or right child is None type or not.
I have modified the tree class to initialize root in constructor.
Modified Code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self,root):
self.root = root
def insert(self,data):
temp = self.root
new_node = Node(data)
new_node.left = None #remove this as it will be set to none in Node constructor itself.
new_node.right =None #remove this
while temp.left or temp.right:
if new_node.data < temp.data:
if temp.left != None:
temp = temp.left
else:
break
else:
if temp.right !=None:
temp = temp.right
else:
break
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
root = Node(19)
#root.left =Node(12) #insert using insert function of tree class
#root.right = Node(30)
mylist = tree(root)
mylist.insert(12)
mylist.insert(30)
mylist.insert(10)
mylist.insert(20)
mylist.insert(18)
mylist.insert(9)
mylist.display(root)
Your code does not take in consideration when a node has only left or right subtree. While iterating through the nodes you need to check if your left or right child is None type or not.
I have modified the tree class to initialize root in constructor.
Modified Code:
class Node:
def __init__(self,data=0):
self.data = data
self.left = None
self.right = None
class tree:
def __init__(self,root):
self.root = root
def insert(self,data):
temp = self.root
new_node = Node(data)
new_node.left = None #remove this as it will be set to none in Node constructor itself.
new_node.right =None #remove this
while temp.left or temp.right:
if new_node.data < temp.data:
if temp.left != None:
temp = temp.left
else:
break
else:
if temp.right !=None:
temp = temp.right
else:
break
if temp.data > new_node.data:
temp.left = new_node
else:
temp.right = new_node
def display(self,temp):
if temp == None:
return
else:
print(temp.data,end = " ")
self.display(temp.left)
self.display(temp.right)
if __name__=='__main__':
root = Node(19)
#root.left =Node(12) #insert using insert function of tree class
#root.right = Node(30)
mylist = tree(root)
mylist.insert(12)
mylist.insert(30)
mylist.insert(10)
mylist.insert(20)
mylist.insert(18)
mylist.insert(9)
mylist.display(root)
edited Jan 2 at 5:48
answered Jan 2 at 4:40
binfbinf
6113
6113
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
add a comment |
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
how does this answer OP's question?
– Oswald
Jan 2 at 4:46
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Thanks. Can you explain me what is meant by this line : self.root = root
– Drool
Jan 2 at 7:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
Just initializing the root node in constructor. Once the tree object is created, we can refer root node within the class with : self.root. You were using the root as a global variable and i have initialized it in class constructor so that each tree object can have a root node of their own. Now you can create multiple tree objects and initialize their root node. To display we can use something like this: mylist.display(mylist.root). similarly it can used for other tree objects without using the variable.
– binf
Jan 2 at 8:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001005%2fgetting-no-display-of-the-output-when-inserting-a-node-in-a-bst%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown