二叉树

本文介绍了一种使用Java实现二叉树的先序、中序和后序遍历的方法,并通过具体实例展示了如何创建二叉树结构,以及如何进行遍历和计算树的深度与叶子节点数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package pack;

class Node {
    char val;
    Node lchild;
    Node rchild;
    public Node(char var) {
        this.val = var;
    }
}

interface Tree {
    public abstract Node init();
    public abstract void preRead(Node head);
    public abstract void midRead(Node head);
    public abstract void lasRead(Node head);
}

public class Mian {
    public static void main(String[] args) {
        Tree t = new Tree() {
            @Override
            public Node init() {
                Node node1 = new Node('A');
                Node node2 = new Node('B');
                Node node3 = new Node('C');
                Node node4 = new Node('D');
                Node node5 = new Node('E');
                Node node6 = new Node('F');
                Node node7 = new Node('G');
                Node node8 = new Node('H');
                Node node9 = new Node('I');
                Node node10 = new Node('J');
                Node node11 = new Node('K');
                node1.lchild = node2; node1.rchild = node3;
                node2.lchild = node4; node2.rchild = node5;
                node3.lchild = node6; node3.rchild = node7;
                node4.lchild = node8; node8.rchild = node11;
                node6.lchild = node9;
                node7.rchild = node10;
                return node1;
            }

            @Override
            public void preRead(Node head) {
                if(head == null)
                    return;
                System.out.println(head.val);
                preRead(head.lchild);
                preRead(head.rchild);
            }

            @Override
            public void midRead(Node head) {
                if(head == null)
                    return;
                preRead(head.lchild);
                preRead(head.rchild);
                System.out.println(head.val);
            }

            @Override
            public void lasRead(Node head) {
                if(head == null)
                    return;
                System.out.println(head.val);
                preRead(head.lchild);
                preRead(head.rchild);
            }

        };
        t.preRead(t.init());
    }
}
#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<stack>

struct Node {
    int data;
    Node *lChild;
    Node *rChild;
};

//先序遍历:根节点、左结点、右结点
void preOrder(Node *root) {
    if (root != NULL) {
        cout << root->data << endl;
        preOrder(root->lChild);  //遍历左子树
        preOrder(root->rChild);  //遍历右子树
    }       
}


//中序遍历:左结点、根节点、右结点
void inOrder(Node *root) {
    if (root != NULL) {
        inOrder(root->lChild);  //遍历左子树
        cout << root->data << endl;
        inOrder(root->rChild);  //遍历右子树
    }
}

//后序遍历:左结点、右结点、根节点
void postOrder(Node *root) {
    if (root != NULL) {
        postOrder(root->lChild);  //遍历左子树
        postOrder(root->rChild);  //遍历右子树
        cout << root->data << endl;
    }
}

//求叶子个数(无子结点)
int sum = 0;
void countLeaf(Node *root) {

    if (root != NULL) {
        if (root->lChild == NULL && root->rChild == NULL) { //没子结点
            sum++;
        }
        if (root->lChild)  //有左结点
            countLeaf(root->lChild);
        if (root->rChild)  //有右结点
            countLeaf(root->rChild);
    }
}

//求树深度
int Depth(Node *root) { 
    int deptleft = 0;
    int deptright = 0;
    int dept = 0;

    if (root == NULL) {
        return 0;
    }

    deptleft = Depth(root->lChild); //获取左子树高度
    deptright = Depth(root->rChild); //获取右子数高度 

    dept = 1 + ((deptleft > deptright) ? deptleft : deptright);//左高度和右高度取最大就是树高度
    return dept;

}

int main() {

    Node node1, node2, node3, node4, node5;

    memset(&node1, 0, sizeof(node1));
    memset(&node2, 0, sizeof(node2));
    memset(&node3, 0, sizeof(node3));
    memset(&node4, 0, sizeof(node4));
    memset(&node5, 0, sizeof(node5));

    node1.data = 1;
    node2.data = 2;
    node3.data = 3;
    node4.data = 4;
    node5.data = 5;
    node1.lChild = &node2;
    node1.rChild = &node3;
    node2.lChild = &node4;
    node3.lChild = &node5;

    //preOrder(&node1);
    //inOrder(&node1);
    //countLeaf(&node1);
    cout << Depth(&node1) << endl;

    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值