C语言版二叉树

二叉树

性质

性质1:二叉树第i层上的结点数目最多为 2{i-1} (i≥1)

性质2:深度为k的二叉树至多有2{k}-1个结点(k≥1)。

性质3:包含n个结点的二叉树的高度至少为log2 (n+1)

性质4:在任意一棵二叉树中,若终端结点的个数为n0,度为2的结点数为n2,则n0=n2+1

性质5: 如果对一棵有n个结点的完全二叉树的结点按层序编号(从第1层到第 +1层,每层从左到右),则对任一结点i(1<=i<=n),有:(双亲节点和孩子节点编号的关系)

(1)如果i=1,则结点i无双亲,是二叉树的根;如果i>1,则其双亲是结点。

(2)如果2i>n,则结点i为叶子结点,无左孩子;否则,其左孩子是结点2i。

(3)如果2i+1>n,则结点i无右孩子;否则,其右孩子是结点2i+1。

实现代码:

 /*
  * @Author: your name
  * @Date: 2020-12-18 17:00:51
  * @LastEditTime: 2020-12-18 21:21:40
  * @LastEditors: Please set LastEditors
  * @Description: In User Settings Edit
  * @FilePath: \C-debug\BinaryTree.cpp
  */
 ​
 #include<iostream>
 using namespace std;
 ​
 typedef struct Node{
     int data;
     struct Node *lchild;
     struct Node *rchild;
     Node(){
         lchild = rchild = NULL;
     }
 } Node,*BinaryTree;
 ​
 BinaryTree createTree(BinaryTree tree){
     char in;
     cin >> in;
     if(in=='#'){
         return NULL;
     }else{
         tree=(BinaryTree)malloc(sizeof(Node)); //创建 Node 空间
         tree->data=in;
         tree->lchild=createTree(tree->lchild);/*创建左子树*/
         tree->rchild=createTree(tree->rchild);/*创建右子树*/
         return tree;
     }
 }
 ​
 // 创建遍历的方法
 void visit(char data){
     cout << data << " ";
 }
 ​
 void preOrder(BinaryTree tree){
     if(tree!=NULL){
         visit(tree->data);
         preOrder(tree->lchild);
         preOrder(tree->rchild);
     }
 }
 ​
 void inOrder(BinaryTree tree){
     if(tree!=NULL){
         inOrder(tree->lchild);
         visit(tree->data);
         inOrder(tree->rchild);
     }
 }
 ​
 void postOrder(BinaryTree tree){
     if(tree!=NULL){
         postOrder(tree->lchild);
         postOrder(tree->rchild);
         visit(tree->data);
     }
 }
 ​
 // 复制二叉树,目前还有一点小问题需要改善
 int Copy(BinaryTree tree,BinaryTree tree1){
     if(tree == NULL){
         tree1 = NULL;
         return 0;
     }else{
         tree1 = new Node;
         tree1->data = tree->data;
         Copy(tree->lchild, tree1->lchild);
         Copy(tree->rchild, tree1->rchild);
     }
 }
 ​
 // 求二叉树节点的个数
 int countNode(BinaryTree tree){
     if(tree==NULL){
         return 0;
     }else{
         return countNode(tree->lchild) + countNode(tree->rchild) + 1;
     }
 }
 ​
 // 求叶子节点的个数
 int leafCount(BinaryTree tree){
     if(tree==NULL){
         return 0;
     }else if(tree->lchild==NULL&&tree->rchild==NULL){
         return 1; 
     }else{
         return leafCount(tree->lchild) + leafCount(tree->rchild);
     }
 }
 // 求树的深度
 int getDepth(BinaryTree tree){
     if(tree==NULL){
         return 0;
     }else{
         int m = getDepth(tree->lchild);
         int n = getDepth(tree->rchild);
         return max(m, n) + 1;
     }
 }
 // ABC##DE#G##F###
 /*
 图是这样的
                  A
              /
             B
          /    \
         C       D
               /
             E
          /   \
         G       F
                 
 */
 ​
 int main(){
     BinaryTree tree = new Node;
     cout << "输入节点的值:" << endl;
     tree = createTree(tree);
     preOrder(tree);
     cout << endl;
     inOrder(tree);
     cout << endl;
     postOrder(tree);
 ​
     cout << endl
          << countNode(tree) << endl;
     cout << endl
          << leafCount(tree) << endl;
     cout << endl
          << getDepth(tree) << endl;
 }


树的存储结构:

  • 树的定义

 

  • 双亲表示法

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值