0% found this document useful (0 votes)
3 views

Data Structures and Algorithms

Summary of note on data structure and algorithm

Uploaded by

Yohans Brhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures and Algorithms

Summary of note on data structure and algorithm

Uploaded by

Yohans Brhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

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.

Case 2: There is data


 Search the appropriate position.
 Insert the node in that position.
III. Tree Traversing:
Binary search tree can be traversed in three ways.
a. Pre order traversal - traversing binary tree in the order of parent, left and right.
b. Inorder traversal - traversing binary tree in the order of left, parent and right.
c. Postorder traversal - traversing binary tree in the order of left, right and parent.

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

PART FOUR: Advanced Sorting and Searching Algorithms


i. Shell Sort: it is an improvement of insertion sort. It creates a reasonable order first. Its
time complexity is O(n3/2)
Algorithm:
1. Choose gap gk between elements to be partly ordered.
2. Generate a sequence (called increment sequence) gk, gk-1,…., g2, g1 where for each
sequence gi, A[j]<=A[j+gi] for 0<=j<=n-1-gi and k>=i>=1
ii. Quick sort: is the fastest known algorithm. It uses divide and conquer strategy and in
the worst case its complexity is O (n2). But its expected complexity is O(nlogn).
Algorithm:
1. Choose a pivot value (mostly the first element is taken as the pivot value)
2. Position the pivot element and partition the list so that:
 the left part has items less than or equal to the pivot value
 the right part has items greater than or equal to the pivot value
3. Recursively sort the left part
4. Recursively sort the right part
iii. Heap Sort
Heap sort operates by first converting the list in to a heap tree. Heap tree is a binary tree in which
each node has a value greater than both its children (if any). It uses a process called "adjust to
accomplish its task (building a heap tree) whenever a value is larger than its parent. The time
complexity of heap sort is O(nlogn).
Algorithm:
1. Construct a binary tree
 The root node corresponds to Data[0].
 If we consider the index associated with a particular node to be i, then the left child of
this node corresponds to the element with index 2*i+1 and the right child corresponds
to the element with index 2*i+2. If any or both of these elements do not exist in the
array, then the corresponding child node does not exist either.
2. Construct the heap tree from initial binary tree using "adjust" process.
Sort by swapping the root value with the lowest, right most value and deleting the lowest, right
most value and inserting the deleted value in the array in it proper position.
iv. Merge sort
Merge sort uses divide and conquer strategy and its time complexity is O(nlogn).
Algorithm:
1. Divide the array in to two halves.
2. Recursively sort the first n/2 items.
3. Recursively sort the last n/2 items.
4. Merge sorted items (using an auxiliary array).

11

You might also like