Create a Dictionary based on the lower ASCII characters 32 (Blank) through 128 (
ID: 3848755 • Letter: C
Question
Create a Dictionary based on the lower ASCII characters 32 (Blank) through 128 (Euro). This will create a binary tree with 96 nodes. Using these techniques, create an encryption key for each character: * Starting at the root of the tree, search for the character. * As the search descends the tree: * If the character goes down the left side of the tree, encrypt a "0" * If the character goes down the right side of the tree, encrypt a "1" * When the character is found, include a consistent termination symbol in the encryption from the lower ASCII character set (for example "~").
For example: Suppose you have a tree of Numbers (0-9): [4] / [2] [5] / [1] [3] [7] / [6] [8] [9] 1: "00~" 2: "0~" 3: "01~" 4: "~" 5: "1~" 6: "110~" 7: "11~" 8: "111~" 9: "1111~" The numbers 1356 encrypted would read: "00~01~1~110~"
Explanation / Answer
inOrder function print Binary Search Tree in inorder.
Here we mimic the BST of array format, so we don't need to store any information about tree.
#include <stdio.h>
void inOrder(int s, int e)
{
if (s>e)
return;
int mid = (s+e)/2;
inOrder(s, mid-1);
printf("%c ", mid+32);
inOrder(mid+1, e);
}
void recur(int s, int e, int val)
{
int mid = (s+e)/2;
if(mid == val){
printf("~ ");
return;
}
if(val > mid){
printf("1");
recur(mid+1, e, val);
}
else{
printf("0");
recur(s, mid-1, val);
}
}
int main()
{
char ch;
inOrder(0,95);
printf(" Enter char : ");
scanf("%c",&ch);
recur(0,95, ch-32);
}