이 진 트 리 앞 순서 중 순서 후 순서 옮 겨 다 니 기 (링크 재 귀적 실현 C 언어)
2243 단어 데이터 구조
입력: (차원 반복 입력)
6 2 1 0 0 4 3 0 0 0 8 0 0
Output:
이 진 트 리 구축 성공 이 진 트 리 총 절 점 수 는 6 이 진 트 리 깊이: 4 이 진 트 리 잎 노드 수: 3 앞 순 서 를 옮 겨 다 니 는 결과: 6 2 1 4 3 8 중간 순 서 를 옮 겨 다 니 는 결과: 1 2 3 4 6 8 후 순서 로 옮 겨 다 니 는 결과: 1 3 4 2 8 6
#include
#include
#include
using namespace std;
typedef int TelemType;
typedef struct BinaryTreeNode
{
TelemType data;
struct BinaryTreeNode *Left;
struct BinaryTreeNode *Right;
}Node;
// , -> ->
Node* createBinaryTree()
{
Node *p;
TelemType ch;
cin>>ch;
if(ch == 0) // , 、 0
{
p = NULL;
}
else
{
p = (Node*)malloc(sizeof(Node));
p->data = ch;
p->Left = createBinaryTree(); //
p->Right = createBinaryTree(); //
}
return p;
}
//
void preOrderTraverse(Node* root)
{
if( root )
{
cout<data<Left);
preOrderTraverse(root->Right);
}
}
//
void inOrderTraverse(Node* root)
{
if( root )
{
inOrderTraverse(root->Left);
cout<data<Right);
}
}
//
void lastOrderTraverse(Node* root)
{
if( root )
{
lastOrderTraverse(root->Left);
lastOrderTraverse(root->Right);
cout<data<Left)+Nodenum(root->Right);
}
}
//
int DepthOfTree(Node* root)
{
if(root)
{
return DepthOfTree(root->Left)>DepthOfTree(root->Right)?DepthOfTree(root->Left)+1:DepthOfTree(root->Right)+1;
}
if( root == NULL )
{
return 0;
}
}
//
int Leafnum(Node* root)
{
if(!root)
{
return 0;
}
else if( (root->Left == NULL) && (root->Right == NULL) )
{
return 1;
}
else
{
return (Leafnum(root->Left) + Leafnum(root->Right)) ;
}
}
int main()
{
Node *root = NULL;
root = createBinaryTree();
printf(" ");
cout<