当前位置:编程学习 > C/C++ >>

二叉树操作

[cpp] 】
#include<stdio.h>  
#include<stdlib.h>  
#include<assert.h>  
 
struct tnode 

    int data; 
    struct tnode *lchild; 
    struct tnode *rchild; 
}tnode; 
typedef struct tnode * TNode; 
//此程序中没用上  
TNode newNode(int data) 

    TNode node = (TNode)malloc(sizeof(struct tnode)); 
    node->data = data; 
    node->lchild = NULL; 
    node->rchild = NULL; 
    return node; 

 
 
TNode createTree(TNode root) 

    int data = 0; 
    scanf("%d", &data); 
    if(data == -1) return NULL; 
    root = (TNode)malloc(sizeof(struct tnode)); 
    root->data = data; 
    root->lchild = NULL; 
    root->rchild = NULL; 
    root->lchild = createTree(root->lchild);//如果不赋值给root->lchild则左子树不会被创建,root->lchild依然会是NULL  
    root->rchild = createTree(root->rchild); 
    return root; 

 
void preorderTraverse(TNode root) 

    if(root != NULL) 
    { 
        printf("%d ", root->data); 
        preorderTraverse(root->lchild); 
        preorderTraverse(root->rchild); 
    } 

 
int treeDepth(TNode root) 

    if(root == NULL) 
        return 0; 
    int nleft = treeDepth(root->lchild); 
    int nright = treeDepth(root->rchild); 
    return (nleft > nright) ? (nleft + 1) : (nright + 1); 

 
void treeFree(TNode root) 

    if(root != NULL) 
    { 
        treeFree(root->lchild); 
        treeFree(root->rchild); 
        printf("%d ", root->data); 
        free(root); 
    } 

 
void test() 

    TNode root = NULL; 
    root = createTree(root); 
    preorderTraverse(root); 
    printf("\n"); 
    int depth = treeDepth(root); 
    printf("depth = %d\n", depth); 
    treeFree(root); 

 
int main() 

    test(); 
    return 0; 

/*****************************\
               1
        2              6
    -1      5       4     -1
         -1  -1  -1  -1  
\*****************************/ 

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

struct tnode
{
 int data;
 struct tnode *lchild;
 struct tnode *rchild;
}tnode;
typedef struct tnode * TNode;
//此程序中没用上
TNode newNode(int data)
{
 TNode node = (TNode)malloc(sizeof(struct tnode));
 node->data = data;
 node->lchild = NULL;
 node->rchild = NULL;
 return node;
}


TNode createTree(TNode root)
{
 int data = 0;
 scanf("%d", &data);
 if(data == -1) return NULL;
 root = (TNode)malloc(sizeof(struct tnode));
 root->data = data;
 root->lchild = NULL;
 root->rchild = NULL;
 root->lchild = createTree(root->lchild);//如果不赋值给root->lchild则左子树不会被创建,root->lchild依然会是NULL
 root->rchild = createTree(root->rchild);
 return root;
}

void preorderTraverse(TNode root)
{
 if(root != NULL)
 {
  printf("%d ", root->data);
  preorderTraverse(root->lchild);
  preorderTraverse(root->rchild);
 }
}

int treeDepth(TNode root)
{
 if(root == NULL)
  return 0;
 int nleft = treeDepth(root->lchild);
 int nright = treeDepth(root->rchild);
 return (nleft > nright) ? (nleft + 1) : (nright + 1);
}

void treeFree(TNode root)
{
 if(root != NULL)
 {
  treeFree(root->lchild);
  treeFree(root->rchild);
  printf("%d ", root->data);
  free(root);
 }
}

void test()
{
 TNode root = NULL;
 root = createTree(root);
 preorderTraverse(root);
 printf("\n");
 int depth = treeDepth(root);
 printf("depth = %d\n", depth);
 treeFree(root);
}

int main()
{
 test();
 return 0;
}
/*****************************\
               1
  2              6
    -1      5       4     -1
         -1  -1  -1  -1 
\*****************************/

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,