Data Structures and Algorithms
Data Structures and Algorithms
TREE STRUCTURES
A tree is a set of nodes and edges that connect pairs of nodes that connect pairs of nodes. It is an
abstract model of a hierarchical structure. Rooted tree has the following structure:
One node distinguished as root.
Every node C except the root is connected from exactly other node P. P is C's parent,
and C is one of C's children.
There is a unique path from the root to the each node.
The number of edges in a path is the length of the path.
Tree Terminologies:
Root: a node without a parent.
Internal node: a node with at least one child.
External (leaf) node: a node without a child.
Ancestors of a node: parent, grandparent, grand-grandparent, etc of a node.
Descendants of a node: children, grandchildren, grand-grandchildren etc of a node.
Depth of a node: number of ancestors or length of the path from the root to the node.
Height of a tree: depth of the deepest node.
Subtree: a tree consisting of a node and its descendants.
Binary tree: a tree in which each node has at most two children called left child and right child.
Binary search tree (ordered binary tree): a binary tree that may be empty, but if it is not empty it
satisfies the following.
o Every node has a key and no two elements have the same key.
o The keys in the right subtree are larger than the keys in the root.
o The keys in the left subtree are smaller than the keys in the root.
o The left and the right subtrees are also binary search trees.
Operations on Binary Search Tree
I. Tree structure definition:
Example: struct Node{
int Num; // Node value/Number
Node * Left, *Right; // Pointer to Left and right Children
};
Node *RootNodePtr=NULL; // pointer to root Node
II. Insertion
When a node is inserted the definition of binary search tree should be preserved.
Case 1: There is no data in the tree (i.e. RootNodePtr is NULL)
The node pointed by InsNodePtr should be made the root node.
9
IV. Searching
To search a node (whose Num value is Number) in a binary search tree (whose root node is
pointed by RootNodePtr), one of the three traversal methods can be used.
V. Deletion
To delete a node (whose Num value is N) from binary search tree (whose root node is pointed by
RootNodePtr), four cases should be considered. When a node is deleted the definition of binary
search tree should be preserved.
Case 1: Deleting a leaf node (a node having no child): it is simple and straightforward operation.
Delete the leaf node and make the pointer of the deleted node parent NULL (right or left child
pointer based on whether the deleted child is left child or right child)
Case 2: Deleting a node having only one child
Case 3: Deleting a node having two children
Case 4: Deleting the root node
Note: For case 2, 3 and 4 there are two approaches
Approach 1: Deletion by copying
Copy the node containing the largest element in the left (or the smallest element in the right)
to the node containing the element to be deleted
Delete the Copied Node
Approach 2: Deletion by merging
If deleted node is Root:
the root node pointer is made to point to the right child or left child
the left child/right child of the root node is made the left child/right child of the node
containing the smallest element/largest in the right/left of the root node respectively.
If deleted node has only one child:
Promote a child (left or right) of deleted node as a child (left or right) for the parent of the
deleted node keeping the definition of BST.
If deleted node has two children:
10
Promote a child (left or right) of deleted node as a child for the parent of the deleted node.
The other child of the deleted node is made the left child of the node containing smallest
element in the right of the deleted node or the right child of the deleted node is made the
right child of the node containing largest element in the left of the deleted node
11