Tan
Tan

Reputation: 1583

Dealing with "Keys" in Binary Search Trees

Should I always, use some data as a key value while dealing with the Binary Search trees? I'm asking this becasue I would need key at some point of time if I want to search an element in the tree. Is thre any other alternative?

For example, please consider the following code:

class Node {

int iData;   // data used as key value
double fData; // other data

Node leftChild; // this node's left child

Node rightChild; // this node's right child

}

My Second Question:

Is there any way I can find elements in a Binary Tree as Binary Tree doesn't have any property just like Binary Search Tree where left node of parent must be less than the parent and right node must be greater.

Upvotes: 2

Views: 1059

Answers (1)

gerrytan
gerrytan

Reputation: 41143

  1. I don't see why you always need a key. You can just use double fData as the value you use to determine which node is bigger / smaller (if your requirement suits).
  2. I believe Binary Seach Tree is a Binary Tree where the elements are ordered. Hence nodes on a Binary Tree is not necessarily ordered. So yes you can still find elements on a Binary Tree -- but you have to scan the entire node. You lose the performance benefit of a BST

Upvotes: 3

Related Questions