Interview finishing - binary sorting tree c language and java example

What is a binary sort tree?

A binary search tree (bst) is a special kind of binary tree in which each node has a key value and satisfies the following two requirements:

  1. For any node x, the key values ​​of all nodes on its left subtree are less than the key value of x.

  2. For any node x, the key value of all nodes on its subtree is greater than the key value of x.

Due to the above two properties, a binary sort tree is also called an ordered binary tree. When performing operations such as search, **, and deletion, the binary sorting tree can maintain balance, making the time complexity become O(log n) level, which greatly improves the operation efficiency.

Binary Sorting Tree Principle

  1. Each node in a binary sort tree has at most two subtrees, and all keys (node ​​values) of the left subtree are smaller than the keys of the current node, and all keys of the right subtree are greater than the keys of the current node.
  2. The left and right subtrees are also binary sorted trees.
  3. When a node is searched, compare the size of the keyword to be searched with the keyword of the current node. If they are equal, the search is successful; otherwise, recursively search the left or right subtree according to the size relationship until the corresponding node is found. point or confirm that the node does not exist.
  4. When a new node is created, compare the pending node with the current node from the root node, and recurse left or right to the leaf node according to the size relationship. Then insert the new node to the left or right subtree of the leaf node.
  5. When deleting a node, find the node to be deleted according to the search method. If the node has no child nodes, just delete it directly; The subtree is directly replaced at the node position; if there is both a left subtree and a right subtree, find the predecessor or successor of the node (the smallest node greater than the current node or the largest node smaller than the current node during inorder traversal) node), and then use its value to replace the value of the node to be deleted, and delete the predecessor or successor.

Sample code to implement binary tree sorting in Java:

// 节点类
class Node {
    
    
    int value;
    Node left, right;

    public Node(int item) {
    
    
        value = item;
        left = right = null;
    }
}

public class BinaryTree {
    
    
    Node root;

    public BinaryTree() {
    
    
        root = null;
    }

    // 将给定节点的值插入到二叉树中
    private Node insertNode(Node root, int value) {
    
    
        if (root == null) {
    
    
            root = new Node(value);
            return root;
        }

        if (value < root.value) {
    
    
            root.left = insertNode(root.left, value);
        } else if (value > root.value) {
    
    
            root.right = insertNode(root.right, value);
        }

        return root;
    }

    // 中序遍历,将节点的值按升序返回。
    private void traverseInOrder(Node root, List<Integer> values) {
    
    
        if (root != null) {
    
    
            traverseInOrder(root.left, values);
            values.add(root.value);
            traverseInOrder(root.right, values);
        }
    }

    // 对外的排序方法,接收一个整数数组并返回升序排列后的结果。
    public List<Integer> sort(int[] arr) {
    
    
        for (int i : arr) {
    
    
            root = insertNode(root, i);
        }

        List<Integer> sortedList = new ArrayList<>();
        traverseInOrder(root, sortedList);

        return sortedList;
    }

    // 测试代码
    public static void main(String[] args) {
    
    
        BinaryTree tree = new BinaryTree();
        int[] arr = {
    
    5, 3, 7, 1, 9};

        List<Integer> sortedList = tree.sort(arr);
        System.out.println(sortedList); // Output: [1, 3, 5, 7, 9]
    }
}

In this example, we use the node class Nodeto represent each node in the binary tree. The binary tree class BinaryTreecontains three methods: inserting nodes, inorder traversal and sorting

Sample code for implementing Binary Search Tree Sorting in C language:

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

// 定义二叉树节点结构体
struct Node {
    
    
    int data;           // 数据
    struct Node* left;  // 左子节点指针
    struct Node* right; // 右子节点指针
};

// 创建新节点
struct Node* newNode(int data) {
    
    
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

// 插入节点
struct Node* insert(struct Node* node, int data) {
    
    
    if(node == NULL)
        return newNode(data);
    if(data < node->data)
        node->left = insert(node->left, data);
    else if(data > node->data)
        node->right = insert(node->right, data);
    return node;
}

// 遍历节点并按序排列输出结果 
void inorderTraversal(struct Node* node) {
    
    
    if(node != NULL) {
    
    
        inorderTraversal(node->left);
        printf("%d ", node->data);
        inorderTraversal(node->right);
    }
}

// 测试示例
int main() {
    
    
    int arr[] = {
    
    7, 5, 1, 8, 3, 6, 0, 9, 4, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    struct Node* root = NULL;

    for(int i=0; i<n; i++)
        root = insert(root, arr[i]);

    inorderTraversal(root);

    return 0;
}

The above is a simple binary search tree sorting implementation, which can be modified and optimized as needed.

Guess you like

Origin blog.csdn.net/fumeidonga/article/details/130332392