#include #include #include #include typedef unsigned long long int Lint; typedef struct { Lint cnt; int **seq; } seq; typedef struct _node { int key; struct _node *L, *R; } tnode; int *genlist ( int n ) { int *A, i, j, t; A = (int *)malloc(n * sizeof(int)); A[0] = 100 + rand() % 500; for (i=1; i key) return T; q = p; p = (x < p -> key) ? p -> L : p -> R; } p = (tnode *)malloc(sizeof(tnode)); p -> key = x; p -> L = p -> R = NULL; if (q == NULL) return p; if (x < q -> key) q -> L = p; else q -> R = p; return T; } tnode *BSTcons ( int *A, int n ) { int i; tnode *T; T = NULL; for (i=0; i key); preorder(T->L); preorder(T->R); } void inorder ( tnode *T ) { if (T == NULL) return; inorder(T->L); printf("%-5d", T-> key); inorder(T->R); } void BSTprn ( tnode *T ) { printf(" Preorder : "); preorder(T); printf("\n"); printf(" Inorder : "); inorder(T); printf("\n"); } void BSTfree ( tnode *T ) { if (T == NULL) return; BSTfree(T -> L); BSTfree(T -> R); free(T); } int BSTsame ( tnode *T, tnode *T1 ) { if ((T == NULL) && (T1 == NULL)) return 1; if (T == NULL) return 0; if (T1 == NULL) return 0; if (T -> key != T1 -> key) return 0; return ((BSTsame(T->L,T1->L)) && (BSTsame(T->R,T1->R))) ? 1 : 0; } void checkall ( seq S, int n, tnode *T ) { Lint i, nprob; tnode *T1; nprob = 0; for (i=0; i 1) ? atoi(argv[1]) : 10; printf("%d\n", n); A = genlist(n); printf("\n+++ Sequence count\n"); printf(" Total number of sequences = %Lu\n", countseq(A,n)); printf("\n+++ All sequences\n"); S = findallseq(A,n); prnallseq(S,n); T = BSTcons(A,n); printf("\n+++ BST constructed from input array\n"); BSTprn(T); printf("\n+++ Checking all sequences\n"); checkall(S,n,T); BSTfree(T); for (i=0; i