Binary tree insert.
Posted: Sun Sep 23, 2012 11:37 pm
Ok I have one last problem...
I have a binary tree like this....
yes goes left and no goes right
I have created a second tree that looks like this....
So i have traversed my tree and found the answer 'answ3'. Now i have added a new question and a new answer.
I want to insert that newQuestion to the leaf where ans3 is/was.
I have found the answ3 so i was thinking of doing a search through my tree for that leaf. Then i need to replace that leaf with my new node which i am stumped at.
So it's essentially an insert function but I don't know how to go about it. Can I step through the tree and then cons or append it when i reach the right spot?
Thanks for the help.
just to clarify this will be the new tree...
I have a binary tree like this....
Code: Select all
(question1 (question2 (question4 (answ3) (answ4) ) (question5 (answ5) (answ6) ) ) (question3 (answ1) (answ2) ) )
Code: Select all
question1
/ \
question2 question3
/ \ / \
question4 question5 answ1 answ2
/ \ / \
answ3 answ4 answ5 answ6
I have created a second tree that looks like this....
Code: Select all
(newQuestion (answ3)(newans))
Code: Select all
newQuestion
/ \
answ3 newAns
I want to insert that newQuestion to the leaf where ans3 is/was.
I have found the answ3 so i was thinking of doing a search through my tree for that leaf. Then i need to replace that leaf with my new node which i am stumped at.
So it's essentially an insert function but I don't know how to go about it. Can I step through the tree and then cons or append it when i reach the right spot?
Thanks for the help.
just to clarify this will be the new tree...
Code: Select all
question1
/ \
question2 question3
/ \ / \
question4 question5 answ1 answ2
/ \ / \
newQues answ4 answ5 answ6
/ \
answ3 newAns