/// <summary> /// 전위 순회 함수 /// 전위 순회는 뿌리 ->왼쪽 -> 오른쪽 순서로 방문 /// </summary> public void PreorderPrintTree() { JeongBinaryTreeNode <T> current = Root; JeongStack <JeongBinaryTreeNode <T> > BtreeStaks = new JeongStack <JeongBinaryTreeNode <T> >(); JeongLinkedList <T> printList = new JeongLinkedList <T>(); if (Root == null) { Console.Write("리턴 합니다 트리가 비었습니다"); return; } BtreeStaks.PuSH(Root); //BtreeStaks.PuSH(Root); Console.WriteLine(BtreeStaks.IsEmpTy()); int index = 0; while (!BtreeStaks.IsEmpTy()) { var PopData = BtreeStaks.Pop; printList.Add(PopData.Data); // 방문 처리 //스택은 FIFO // 오른쪽 먼저 삽입 -> 왼쪽 삽입 if (PopData.Right != null) { BtreeStaks.Push = PopData.Right; } if (PopData.Left != null) { BtreeStaks.Push = PopData.Left; } } ConsolPrint(ref printList); }
/// <summary> /// 중위 순회 /// </summary> public void InorderPrintTree() { JeongBinaryTreeNode <T> current = null; JeongStack <JeongBinaryTreeNode <T> > BtreeStaks = new JeongStack <JeongBinaryTreeNode <T> >(); JeongLinkedList <T> printList = new JeongLinkedList <T>(); BtreeStaks.PuSH(current); current = Root; //BtreeStaks.PuSH(Root); Console.WriteLine(BtreeStaks.IsEmpTy()); while (!BtreeStaks.IsEmpTy()) { // 매 반복시 왼쪾 트리 검색 만약 왼쪽 트리 원소가 있다면 스택에 저장 while (current != null) { BtreeStaks.Push = current; current = current.Left; } current = BtreeStaks.Pop; if (current == null) { Console.WriteLine("현재 널 발생"); break; } // break; printList.Add(current.Data); current = current.Right; Console.WriteLine("현재 널 발생2"); } ConsolPrint(ref printList); }
/// <summary> /// ㅎ후위 순회 /// </summary> public void postorderPrintTree() { JeongBinaryTreeNode <T> current = null; JeongBinaryTreeNode <T> doneNode = null; JeongStack <JeongBinaryTreeNode <T> > BtreeStaks = new JeongStack <JeongBinaryTreeNode <T> >(); JeongLinkedList <T> printList = new JeongLinkedList <T>(); //BtreeStaks.PuSH(current); current = Root; //BtreeStaks.PuSH(Root); Console.WriteLine(BtreeStaks.IsEmpTy()); string stackPath = ""; int i = 0; while (true) { if (current != null && current != doneNode) { // 매 반복시 왼쪾 트리 검색 만약 왼쪽 트리 원소가 있다면 스택에 저장 //stackPath += current.Data + " -> "; BtreeStaks.Push = current; while (current != null) { if (current.Right != null) { BtreeStaks.Push = current.Right; //stackPath += current.Right.Data + " -> "; } if (current.Left != null) { BtreeStaks.Push = current.Left; //stackPath += current.Left.Data + " -> "; } current = current.Left; } } int sa = 1; //Console.WriteLine((i++) + " 회차 스택 뷰 \n"); //treeStaks.StackView(); if (!BtreeStaks.IsEmpTy()) { current = BtreeStaks.Pop; // 스택에서 노드 뽑기 // 왼쪽 노드가 널이 아니고 오른쪽 노드가 널인경우 if (current.Left != null && current.Right == null && current.Left != doneNode) { BtreeStaks.Push = current; // stackPath += current.Data + "|| -> "; current = current.Left; } if (current.Right == null || current.Right == doneNode) { printList.Add(current.Data); doneNode = current; } // Console.WriteLine("done Node :: " + doneNode.Data); } else { break; } //Console.WriteLine(stackPath); } ConsolPrint(ref printList); }