Implement binary tree Data Structure with 3 fields
Implement binary tree Data Structure with 3 fields tyepdef struct { int key; } element; typedef struct node *tree. Pointer; typedef struct { element data; tree. Pointer left. Child, right. Child; } node; node left. Child data right. Child NULL 2 NULL 데이터 마이닝 연구실 2021 -12 -22
Node insert If tree is NULL , insert node at the root If tree is not NULL (Kc, Kn : keys of current node and new node) 3 if (Kc > Kn) insert it in left. Child else if (Kc < Kn ) insert it in right. Child else nothing 데이터 마이닝 연구실 2021 -12 -22
Example of binary search tree Root 10 5 14 3 1 8 4 Insert order : 10 4 17 11 15 14 5 3 4 1 데이터 마이닝 연구실 11 17 8 15 2021 -12 -22
Tree traversal Left. Child , Visit, Right. Child (L, V, R) Inorder traversal : LVR Preorder traversal : VLR Postorder traversal : LRV Ex) 5 Void inorder(tree. Pointer ptr) { if ( ptr ) { inorder(ptr->left. Child); printf(“%d”, ptr->element. key); inorder(ptr->right. Child); } } 데이터 마이닝 연구실 2021 -12 -22
Home work Make a BST with 10 data showed page 4 Print key values using three methods Inorder traversal Preorder traversal Postorder traversal Which method can print the key as an ascending order ? Explain it with result How can we print the key as a descending order? 6 If it’s possible , implement the source code and print it 데이터 마이닝 연구실 2021 -12 -22
- Slides: 6