#include
// an array to store each value of the node in the tree by its indexint values [maxsize];
// Node structure constructORTYPEDEF STRUCT BT {ElementType Data; Struct Bt * lchild, * rchild;} binarytreenode, * btroot;
// Function DecleArelementType Inorder (BTROOT ROOT); ElementType PostOrder (BTROOT ROOT); Void ListTree (int L); Void Addspace (INT i);
// the main function to exchangemain () {INT I, L, N;
Btroot r = malloc (sizeof (binarytreenode)); printf ("===================================== ================================ / n "); Printf (" BinaryTree List & 3-ORDER Program by Songnan @ 2004 / N "); Printf (" ========================================== =======================================); Printf ("" The Following Instructions CAN Help You To Use IT: "); Printf (" / N "); Printf (" 1.To Tell How Many Level (S) You Want To Use: (1-9) "); Printf (" 2.You SHOULD GIVE THE VALUE TO The CURRENT ROOT (Even A Root of Sub ) / N "); Printf (" 3.if you want to create the left child for the current node press y / NOTHERWISE, N / N "); Printf (" 4.Use the Same Way to create the right child for the Current node / n "); Printf (" See the resultiff ==); printf ("======================= ======================================================= / n "); Printf ("/ n");
Printf ("How Many Levels of The Tree You Want TO CREATE?"); Scanf ("% D", & L); N = POW (2, L) -1; for (i = 0; i <= n; i ) {VALUES [I] = - 1;} CreateTree (R, 1); Printf ("============================= ================================================ / n "); Printf (" / npreorder: " Preorder (R); Printf ("/ ninorder:"); inorder (r); Printf ("/ npostorder:"); PostORDER (R); Printf ("/ n"); printf ("==== ============================================================================================================================================================================================================= ============= / n "); Printf (" THE Constructor of the Tree Comes Here: / N "); ListTree (L); Printf (" =================================================================================================================================== ============================================================================================================================================================================================================= ========= / n "); Printf (" (ps: the value -1 means no such a node exist) / n "); printf (" =========== ====================
============================================== / n "); Printf (" Copyright by Songnan @ 2004.11 / N "); Printf (" All Codes & Program "); // FOR (i = 0; I <31; I ) // Printf ("% D / N ", VALUES [I]); while (1); Return 0;} // inorder functioninorder (btroot root) {if (! root) returnography; inorder (root-> lchild); Printf ("% 4D", root-> data); inorder (root-> rchild); }
// Preorder FunctionPreorder (BTROOT ROOT) {if (! root) Return 0; Printf ("% 4D", root-> data); preorder (root-> lchild); preorder (root-> rchild);}
// PostORDER FUNCTIONPOSTORDER (BTROOT ROOT) {if (! root) Return 0; Postorder (root-> lchild); postorder (root-> rchild); Printf ("% 4D", root-> data);} / * Receive THE INPUT DATA and CREATE A Node Each Time! * / CreateTree (btroot root, int nodenum) / * preorder * / {ElementType X; int Layer; char C;
C = 'a'; layer = log (nodenum) / log (2) 1; Printf ("/ Ninput the root's value of the subtree% D:", nodenum); scanf ("% d", & x); root -> DATA = X; VALUES [nodenum] = x;
Printf ("WANNA CREATE LEFTCHILD FOR NODE% D? (Y / N)", NodeNum); Scanf ("% 1S", & C); if (c == 'n' || c == 'n') root > lchild = null; else {root-> lchild = (btroot) malloc (sizeof (binarytreenode)); CreateTree (root-> lchild, 2 * nodenum);}
Printf ("WANNA CREATE RIGHTCHILD for NODE% D? (Y / N)", NODENUM); Scanf ("% 1S", & C); if (c == 'n' || c == 'n') root > rchild = null; else {root-> rchild = (btroot) malloc (sizeof (binarytreenode)); CreateTree (root-> rchild, 2 * nodenum 1);} return 0;} // To display the entire Tree on The Screenvoid Listtree (int LV) {Int lv, count, i = 1;
For (lv = 0; lv For (i = 0; i