1 Tree&BinarySearchTree
1 Tree&BinarySearchTree
Tree represents the nodes connected by edges. We will discuss binary tree or
binary search tree specifically.
Binary Tree
Binary Tree is a special data structure used for data storage purposes. A binary tree
has a special condition that each node can have a maximum of two children. A
binary tree has
the benefits of both an ordered array and a linked list as search is as quick as in a
sorted array and insertion or deletion operation are as fast as in linked list.
Important Terms
Following are the important terms with respect to tree.
Path − Path refers to the sequence of nodes along the edges of a tree.
Root – The node at the top of the tree is called root. There is only one root per
tree and one path from the root node to any node.
Parent − Any node except the root node has one edge upward to a node called
parent.
Child – The node below a given node connected by its edge downward is
called its child node.
Leaf – The node which does not have any child node is called the leaf node.
Subtree − Subtree represents the descendants of a node.
Visiting − Visiting refers to checking the value of a node when control is on
the node.
Traversing − Traversing means passing through nodes in a specific order.
Levels − Level of a node represents the generation of a node. If the root node is
at level 0, then its next child node is at level 1, its grandchild is at level 2, and so
on.
Keys − Key represents a value of a node based on which a search operation is
to be carried out for a node.
1. If c is a null reference, then parent will be the parent of n. If n's value is less
than parent's value, then n will be parent's new left child; otherwise n will
be parent's new right child.
2. Compare c and n's values.
3. If c's value equals n's value, then the user is attempting to insert a duplicate
node. Either simply discard the new node, or raise an exception. (Note that
the nodes' values in a BST must be unique.)
4. If n's value is less than c's value, then n must end up in c's left subtree.
Let parent equal c and c equal c's left child, and return to step 1.
5. If n's value is greater than c's value, then n must end up in c's right subtree.
Let parent equal c and c equal c's right child, and return to step 1.
The First Step: Creating Node Class
• number. This member variable contains the data stored in the node of the
type interger.
• rightLeaf, of type Node This member variable represents the node's right
children.
• leftLeaf, of type Node This member variable represents the node's left
children.
Method insertData works in recursive way to insert a new node in binary tree, It
check the value of Node and decide to insert the new node to left or right.
Method search works also in recursive way, searching for node with required
value.
class Node
{
private int number;
public Node rightLeaf;
public Node leftLeaf;
}
else if (node.number < data)
{
insertData(ref node.rightLeaf, data);
}
if (node.number == s)
{
return true;
}
else if (node.number < s)
{
return search(node.rightLeaf, s);
}
else if (node.number > s)
{
return search(node.leftLeaf, s);
}
return false;
}
display(n.leftLeaf);
Console.Write(" " + n.number);
display(n.rightLeaf);
}
class BinaryTree
{
private Node root;
private int count;
public BinaryTree()
{
root = null;
count = 0;
}
public bool isEmpty()
{
return root == null;
}
count++;
}