Implementation and traversal of Java binary tree

A binary tree is the most basic way to implement a tree. Simply put, a binary tree is a tree with at most two subtrees per node.

The subnodes of a binary tree are divided into left and right nodes.

Two Special Binary Trees

Full Binary Tree

Each node must have either two or no children.

Complete Binary Tree

All leaf nodes must be on the last or second to last level.No node can have a right subtree without a left subtree.

Implementation of Binary Tree

// A binary tree node consists of data, left and right nodes
public class BinaryTreeNode{
    private Object data;
    private BinaryTreeNode leftNode;
    private BinaryTreeNode rightNode;

    public BinaryTreeNode(Object data, BinaryTreeNode leftNode, BinaryTreeNode rightNode) {
        this.data = data;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public BinaryTreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(BinaryTreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public BinaryTreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(BinaryTreeNode rightNode) {
        this.rightNode = rightNode;
    }
}

Main methods of binary trees

1. Add Elements

public void insertLeftChild(BinaryTreeNode node){
        this.setLeftNode(node);
    }

    public void insertRightChild(BinaryTreeNode node){
        this.setRightNode(node);
    }

2. Delete elements

Delete an element by setting it to null and recursively null the left and right child nodes.

public void deleteNode(BinaryTreeNode node){
    if (node == null){  //Recursive Exit
        return;
    }
    deleteNode(node.getLeftNode());
    deleteNode(node.getRightNode());
    node = null;
}

3. Get the height of the binary tree

 /**
     * Gets the degree of the specified node
     * @param node
     * @return
     */
    public int getHeight(BinaryTreeNode node){
        if (node == null){      //Recursive Exit
            return 0;
        }
        int leftChildHeight = getHeight(node.getLeftNode());
        int rightChildHeight = getHeight(node.getRightNode());

        int max = Math.max(leftChildHeight, rightChildHeight);

        return max + 1; //Add yourself
    }

4. Get the number of children of a node

public int getSize(BinaryTreeNode node){
    if (node == null){
        return 0;
    }
    int leftNodeSize = getNodeSize(node.getLeftNode());
    int rightNodeSize = getNodeSize(node.getRightNode());

    return leftNodeSize + rightNodeSize + 1;
}

Traversal of Binary Trees

Traversing a binary tree is also a recursive process, so that each node is accessed only once.

It is divided into first-order traversal, middle-order traversal, and then-order traversal, where the first-order is the traversal order of the parent node relative to the left and right child nodes

Preorder traversal

/**
 * Preorder traversal
 * @param node
 */
public void iterateFirstOrder(BinaryTreeNode node){
    if (node == null){
        return;
    }
    operate(node);
    iterateFirstOrder(node.getLeftChild());
    iterateFirstOrder(node.getRightChild());
}

Intermediate traversal

/**
 * Intermediate traversal
 * @param node
 */
public void iterateMediumOrder(BinaryTreeNode node){
    if (node == null){
        return;
    }
    iterateMediumOrder(node.getLeftChild());
    operate(node);
    iterateMediumOrder(node.getRightChild());
}

Post-order traversal

/**
 * Post-order traversal
 * @param node
 */
public void iterateLastOrder(BinaryTreeNode node){
    if (node == null){
        return;
    }
    iterateLastOrder(node.getLeftChild());
    iterateLastOrder(node.getRightChild());
    operate(node);
}

A written test

Given a binary tree's pre-order and middle-order traversal, find its post-order traversal (hint: given pre-order and middle-order traversal can uniquely determine the post-order traversal).

 

 

 

Posted on Fri, 12 Jun 2020 21:00:47 -0400 by mispris006