๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ง๊ฐ๋กœ๊ทธ

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK5 Day 38 -BST ๋ณธ๋ฌธ

Krafton jungle

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK5 Day 38 -BST

habbn 2024. 2. 15. 00:17
728x90

๐Ÿ“†24.2.14

 

์˜ค๋Š˜๋ถ€๋กœ ๊ณผ์ œ ๋‹ค ๋๋ƒˆ๋‹ค 

BST ๋ถ€๋ถ„ ์–ด๋ ค์›Œ์„œ ๊ฑฐ์˜ ๋‹ค ๋‹ต ๋ณด๊ฑฐ๋‚˜ GPT ๋Œ๋ ค์„œ ํ’€์—ˆ๋‹ค....

๊ณผ์ œ ๋๋‚ด๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ’€๊ณ  ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๋ถ€ํ–ˆ๋•….

 

levelOrderTraversal

 

๋ ˆ๋ฒจ ์ˆœ์œผ๋กœ ํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

void levelOrderTraversal(BSTNode* root)
{
	Queue q;			//์ˆœํšŒํ•˜๋ฉฐ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๋“ค์„ ์ž„์‹œ๋กœ ์ €์žฅ
	q.head = NULL;
	q.tail = NULL;

	BSTNode *temp;
	temp = 	root;

	if(temp == NULL)
		return;
	else
	{
		enqueue(&q.head, &q.tail, temp);	//๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ด	
		while(!isEmpty(q.head))
		{
			temp = dequeue(&q.head, &q.tail);	//ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด ํ•ด๋‹น ๋…ธ๋“œ์˜ ๊ฐ’ ์ถœ๋ ฅ
			printf("%d ",temp->item);

			if(temp->left != NULL)
				enqueue(&q.head, &q.tail, temp->left);
			if(temp->right != NULL)
				enqueue(&q.head, &q.tail, temp->right);
		}
	}
}

 

 

inOrderIternative

 

์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ง„ํƒ์ƒ‰ ํŠธ๋ฆฌ๋ฅผ ์ค‘์œ„ ์ˆœํšŒํ•œ๋‹ค. ( left -> root -> right)

void inOrderTraversal(BSTNode *root)
{
	Stack s;
	s.top = NULL;

	BSTNode *temp;
	temp = root;

	if(temp == NULL)	 return;
	
	while(1)
	{
		if(temp != NULL)
		{
			push(&s,temp);
			temp = temp->left;		//์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ด๋™
		}
		else
		{
			if(!isEmpty(&s))	
			{
				temp = pop(&s);				
				printf("%d ", temp->item);
				temp= temp->right;		//์ด์ œ ์˜ค๋ฅธ์ชฝ ํ™•์ธ
			}	
			else 
				break;
		}
	}
}

 

 

preOrderIternative

 

์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „์œ„์ˆœํšŒํ•œ๋‹ค. (root -> left -> right)

void preOrderIterative(BSTNode *root)
{
	Stack s;
	s.top = NULL;

	BSTNode *temp;
	temp = root;

	if(temp == NULL) 	return;
	else
	{
		push(&s, temp);	
		
		while(!isEmpty(&s))
		{
			temp = pop(&s);
			printf("%d ", temp->item);

			if(temp->right != NULL)
				push(&s,temp->right);
			if(temp->left != NULL)
				push(&s,temp->left);
		}
	}
}

 

 

postOrderIternativeS1

 

์Šคํƒ์„ ์‚ฌ์šฉํ•ด์„œ ํ›„์œ„์ˆœํšŒํ•œ๋‹ค. (left -> right -> root)

 

์Šคํƒ์˜ top์— ์žˆ๋Š” ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ , ๋งŒ์•ฝ ์ด ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ null์ด๊ฑฐ๋‚˜ ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ์™€ ๊ฐ™๋‹ค๋ฉด, ์Šคํƒ์˜ top์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ popํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด ๋…ธ๋“œ๋ฅผ ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•œ๋‹ค. ์ด ๊ณผ์ •์€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ํ›„์—๋‚˜ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค.

๋งŒ์•ฝ ์Šคํƒ์˜ top์— ์žˆ๋Š” ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์™€ ๋‹ค๋ฅด๋‹ค๋ฉด, ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ์ด ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•œ๋‹ค. ์ด ๊ณผ์ •์€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. 

void postOrderIterativeS1(BSTNode *root)
{
    if (root == NULL)
        return;

    Stack s;
    s.top = NULL;

    BSTNode *current = root;	//ํ˜„์žฌ ๋ฐฉ๋ฌธ ์ค‘์ธ ๋…ธ๋“œ
    BSTNode *prev = NULL;	    //์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ

    while (current != NULL || !isEmpty(&s))
    {
        if (current != NULL)
        {
            push(&s, current);
            current = current->left;	//์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ๋กœ ์ด๋™
        }
        else
        {
            BSTNode *temp = peek(&s)->right;	//์Šคํƒ์˜ top์— ์žˆ๋Š” ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ ํ™•์ธ	
            if (temp == NULL || temp == prev)	
            {
                temp = pop(&s);
                printf("%d ", temp->item);
                prev = temp;
            }
            else
            {
                current = temp;
            }
        }
    }
}

 

 

postOrderIterativeS2

 

์Šคํƒ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ›„์œ„ ์ˆœํšŒํ•œ๋‹ค

 

์ฒซ ๋ฒˆ์งธ ์Šคํƒ์ด ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋™์•ˆ, ์Šคํƒ์˜ top์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ popํ•˜์—ฌ ๋‘๋ฒˆ์งธ ์Šคํƒ(s2)์— pushํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ์™€ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ฐ๊ฐ ์ฒซ ๋ฒˆ์งธ ์Šคํƒ์— pushํ•œ๋‹ค. ์ด ๊ณผ์ •์„ s1์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด s2์—๋Š” ๋…ธ๋“œ๋“ค์ด ํ›„์œ„ ์ˆœํšŒ ์ˆœ์„œ์˜ ์—ญ์ˆœ์œผ๋กœ ์ €์žฅ๋œ๋‹ค.

->  s2๋Š” ํ›„์œ„ ์ˆœํšŒ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ๋œ๋‹ค. 

void postOrderIterativeS2(BSTNode *root)
{
	Stack s1;
	Stack s2;
	s1.top = NULL;
	s2.top = NULL;

	BSTNode *temp;
	temp = root;

	if(temp ==NULL)	return;
	else
	{
		push(&s1,temp);		//๋ฃจํŠธ ๋…ธ๋“œ push

		while(!isEmpty(&s1))
		{
			temp = pop(&s1);
			push(&s2, temp);

			if(temp->left !=NULL)
				push(&s1,temp->left);
			
			if(temp->right!=NULL)
				push(&s1,temp->right);
		}	
		while(!isEmpty(&s2))
		{
			temp = pop(&s2);
			printf("%d ", temp->item);
		}	
	}	
}

 

 

728x90