0

I need some help with the following problem. There is a Search Tree (Binary Search Tree) and I need to find a particular element, so to search the Search Tree. But this only needs to be displayed in Pseudocode (so the actual search tree isn't needed, so just for an example the root is 75 and the element that needs to be searched is 24).

So for example step 1: Print Root, 2: Print Tree....(until the correct element has been found).

I have done this so far:

1) def findval (node, lookForElement):
2) while node is not null:
3) if node.val is equal to lookForElement:
4) break
5) if node.val is less than lookForElement:
6) node = node.right
7) else:
8) node = node.left
9) return node 10) if node is null: 11) print "tree is empty"

But I think this would only work if the the searched element is the next element down, but the searched element could be a few branches down so therefore how would I correctly loop?

Samual
  • 3
  • 6
  • This will work, but there are no print statements (besides "tree is empty") – Andrew Williamson Jan 19 '16 at 17:36
  • Ok thank you, would this work though? Because it looks like instead of going 75, 50, 30, 24 it would just go 75, 50 and then break, or would it carry on looping? @AndrewWilliamson – Samual Jan 19 '16 at 18:07

1 Answers1

0

Your algorithm to find the node is pretty much correct:

DEF FINDVAL(search)
  LET node = root
  WHILE node != null
    PRINT_ELEMENTS(node)
    IF node.value == search
      BREAK
    ELSE IF node.value < search
      node = node.right
    ELSE
      node = node.left

  RETURN node

The indentation and the space makes it much clearer that the return statement should be outside the WHILE loop. You can only reach it by breaking out of the loop, which happens when you have found the search value (hooray!), or when node becomes null. If node is null, then our desired value is not in the tree.

With the following tree:

      75
    /    \
   18    88
  /  \     \
 6   24    90

The FINDVAL algorithm will display the following:

6, 18, 24, 75, 88, 90
6, 18, 24
24

UPDATE: To insert a new value, if it doesn't already exist:

DEF INSERTVAL(value)
  LET node = root
  WHILE node != null
    PRINT_ELEMENTS(node)
    IF node.value == value
      PRINT("Value already exists")
      RETURN
    ELSE IF node.value < value
      IF node.right == null
        node.right = new Node(value)
        RETURN
      node = node.right
    ELSE
      IF node.left == null
        node.left = new Node(value)
        RETURN
      node = node.left

  root = new Node(value)

This psuedocode may seem a bit harder to understand, because we have to look at two layers in the tree at a time, instead of one. We look at the parent, decide whether to go left or right, and then if that direction is null we insert the new value. If it is not null, we move on to that node and repeat.

Now, the only way to exit the while loop is still by 'node' being null, or by us returning from within the loop. But within the loop, every time we move down a node, first we check if it is null, and we insert and return if it is. So the only time the value of 'node' can be null is if the root is null.

Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
  • Thank you for your detailed answer that is great, so because the loop won't be broken unless the correct node is found is a count not needed? for example every time it checks if the value is < or > then count++ – Samual Jan 19 '16 at 18:29
  • Exactly. A count is normally used as a terminating condition, e.g. when you have reached the end of an array. The WHILE loop in this method terminates when the value of node becomes null, or when we have found the node we are looking for, so we don't need a counter. – Andrew Williamson Jan 19 '16 at 18:40
  • That's what I thought, thank you. I just have one last question - what would the pseudocode be to insert a new element? for example searching the tree, then seeing if 34 exists (using the numbers you have given, as 34 doesnt exist) and if it doesn't then add new element – Samual Jan 19 '16 at 18:42
  • Change the two else statements - when a node doesn't contain the value, look at either the left or the right child like usual. Now, if the child is null, that's where we insert the node with our new value. If it isn't null, we keep traversing the tree. – Andrew Williamson Jan 19 '16 at 18:49
  • Is there any chance you can type it out like your answer so I can understand better? because I would need it to also replace the node, so if 34 does exist then replace it and if it doesn't exist then add it – Samual Jan 19 '16 at 18:51
  • Thank you for all your help. Was just wondering regarding the first search, is there way to say that the tree is empty (for example if the node is null) – Samual Jan 20 '16 at 00:09
  • Yes, if root == null at the beginning, then the tree is empty. – Andrew Williamson Jan 20 '16 at 00:25
  • So where exactly would I place that? (just so I don't mess up the other steps, as it may be useful to state that if the tree is empty then print "tree is is empty" and then move on to the loop), also regarding the insertion what is: root = new Node(value) ? – Samual Jan 20 '16 at 00:39
  • Very first line of the function. Root = new node(value) - that's a special case; when the tree is empty you have to create the root – Andrew Williamson Jan 20 '16 at 00:45
  • So: DEF FINDVAL(search), if root == null, break, LET node = root, WHILE node != null, PRINT_ELEMENTS, – Samual Jan 20 '16 at 00:54
  • If root == null print("Tree is empty"). And you can only use break within a while loop – Andrew Williamson Jan 20 '16 at 01:04
  • Thank you that makes sense, but what if the value isn't found? for example the node would be null if it isn't found so I can't return the node, as it doesn't exist. So where would if node = null print ("Value not found") be place? because the loop only loops if node != null, so what happens if it is and the value isn't in the tree? – Samual Jan 20 '16 at 13:09
  • Check for an empty tree on the very first line of the function, before you even begin to loop. If the tree is empty, you know the following loop will not be executed. If the value isn't in the tree, then the function returns Null. You can return node, even when it is null - this signifies that it doesn't exist. Wikipedia actually has very good examples of binary search tress - https://en.wikipedia.org/wiki/Binary_search_tree. Please read through, and Google or post new questions on SO if you get stuck. – Andrew Williamson Jan 20 '16 at 17:20
  • Note that they write None instead of Null, it makes it a bit easier to read. – Andrew Williamson Jan 20 '16 at 17:24