Data Structure Algorithms: BST, Sort, and CLL Operations
Binary Tree Insertion
- ptr = ROOT, flag = FALSE
- While (ptr != NULL) and (flag = FALSE) do:
- Case: ITEM < ptr→DATA
- ptr1 = ptr
- ptr = ptr→LCHILD
- ptr1 = ptr
- ptr = ptr→RCHILD
- flag = TRUE
- Print “ITEM already exists”
- Exit
- new = GetNode(NODE)
- new→DATA = ITEM
- new→LCHILD = NULL
- new→RCHILD = NULL
- If (ptr1→DATA < ITEM) then
- ptr1→RCHILD = new
- Else
- ptr1→LCHILD = new
- EndIf
Binary Tree Deletion
- ptr = ROOT, flag = FALSE
- While (ptr != NULL) and (flag = FALSE) do:
- Case: ITEM < ptr→DATA
- parent = ptr
- ptr = ptr→LCHILD
- parent = ptr
- ptr = ptr→RCHILD
- flag = TRUE
- Print “ITEM does not exist: No deletion”
- Exit
- case = 1
- If (ptr→LCHILD != NULL) and (ptr→RCHILD != NULL) then:
- case = 3
- Else
- case = 2
- EndIf
Algorithm: SelectionSort(A, n)
- For i = 0 to n – 1 do:
- min_idx = i
- For j = i + 1 to n do:
- If (A[j] < A[min_idx]) then:
- min_idx = j
- EndIf
- j = j + 1
- If (A[j] < A[min_idx]) then:
- EndFor
- If (min_idx != i) then:
- swap(A[min_idx], A[i])
- EndIf
- i = i + 1
Algorithm: InsertionSort(A, n)
- i = 1
- While i < n do:
- key = A[i]
- j = i – 1
- While (j >= 0 and A[j] > key) do:
- A[j + 1] = A[j]
- j = j – 1
- EndWhile
- A[j + 1] = key
- i = i + 1
Algorithm: MergeSort(A, low, high)
Begin
- If (low < high) then:
- mid = (low + high) / 2
- MergeSort(A, low, mid)
- MergeSort(A, mid + 1, high)
- Merge(A, low, mid, high)
- EndIf
End
Algorithm: Merge(A, low, mid, high)
- Set i = low, j = mid + 1, k = 0
- While (i <= mid and j <= high) do:
- If (A[i] <= A[j]) then:
- B[k] = A[i]
- i = i + 1
- Else:
- B[k] = A[j]
- j = j + 1
- EndIf
- k = k + 1
- B[k] = A[i]
- i = i + 1, k = k + 1
- B[k] = A[j]
- j = j + 1, k = k + 1
Circular Linked List (CLL) Operations
Traverse()
- temp = head
- while(temp->link != head):
- print temp->data
- temp = temp->link
Insert_Front()
- newnode = malloc(sizeof(struct node))
- if(head == NULL):
- head = tail = newnode
- newnode->link = head
- newnode->link = head
- head = newnode
- tail->link = head
Insert_End()
- repeat:
- tail->link = newnode
- tail = newnode
- tail->link = head
Insert_After_Key()
- repeat:
- read key
- temp = head
- while(temp->data != key):
- temp = temp->link
- if(temp == head) return
- newnode->link = temp->link
- temp->link = newnode
- if(temp == tail) tail = newnode
Insert_At_Position()
- newnode
- if(head == NULL && pos != 0) return
- read pos, value
- newnode->data = value
- temp = head
- for i = 1 to pos – 1:
- temp = temp->link
- newnode->link = head
- head = newnode
- tail->link = head
Delete_Front()
- if(head == NULL) return
- if(head->link == head):
- free(head)
- head = NULL
- temp = head
- head = head->link
- tail->link = head
- free(temp)
Delete_End()
- repeat:
- head = tail = NULL
- temp = head
- while(temp->link != tail):
- temp = temp->link
- free(tail)
- tail = temp
- tail->link = head
Delete_Key()
- read key
- if(head == NULL) return
- temp = head
- while(temp->data != key && temp->link != head):
- prev = temp
- temp = temp->link
- head = head->link
- tail->link = head
- prev->link = temp->link
- if(temp == tail) tail = prev
Delete_At_Position()
- if(head == NULL) return
- read pos
- if(pos == 1):
- if(head->link == head):
- head = tail = NULL
- else:
- head = head->link
- tail->link = head
- return
- prev = temp
- temp = temp->link
Binary Tree Traversal
Inorder Traversal (Iterative)
Begin
- temp = root
- while (top > -1) or (temp != NULL) do:
- if (temp != NULL) then:
- push(temp)
- temp = temp->left
- else:
- temp = pop()
- print(temp->data)
- temp = temp->right
- endif
- if (temp != NULL) then:
- endwhile
End
Preorder Traversal (Iterative)
Begin
- temp = root
- while (top > -1) or (temp != NULL) do:
- if (temp != NULL) then:
- print(temp->data)
- push(temp)
- temp = temp->left
- else:
- temp = pop()
- temp = temp->right
- endif
- if (temp != NULL) then:
- endwhile
End
Postorder Traversal (Two Stacks)
Begin
- temp = root
- if (root == NULL) then: return endif
- create two stacks s1 and s2
- push(s1, temp)
- while (s1 is not empty) do:
- node = pop(s1)
- push(s2, node)
- if (node->left != NULL) then: push(s1, node->left) endif
- if (node->right != NULL) then: push(s1, node->right) endif
- endwhile
- while (s2 is not empty) do:
- node = pop(s2)
- print(node->data)
- endwhile
End
