Javascript Binary Search Tree

İkili ağaç (Binary Tree), her düğümün sol çocuk ve sağ çocuk olarak adlandırılan en fazla iki çocuğa sahip olduğu bir ağaç veri yapısıdır. Binary Search Tree'nin ikili ağaçtan temel farkı düğümlerde saklanan verilerin sıralı bir şekilde tutulmasıdır. Bunun sayesinde ağaç üzerinde arama yapılmasına olanak sağlar. Binary Search Tree'de eklenen veriler her zaman düğümde yer alan değerden küçük ise sol tarafa, aksi taktirde (büyükse) düğümün sağ tarafına eklenmelidir.

Javascript Binary Search Tree Kodu

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;

    }
}

class BinarySearchTree {
    constructor() {
        this.root = null;
    }

    insert(value) {
        let newNode = new Node(value);

        if (this.root === null) {
            this.root = newNode;
        } else {

            let tempNode = this.root;

            while (true) {

                if (value < tempNode.value) {

                    if (tempNode.left === null) {
                        tempNode.left = newNode;
                        break;
                    }

                    tempNode = tempNode.left;

                } else {
                    if (tempNode.right === null) {
                        tempNode.right = newNode;
                        break;
                    }

                    tempNode = tempNode.right;
                }

            }


        }
    }


    find(value) {
        if (this.root == null) return null;

        let tempNode = this.root;

        while (tempNode !== null) {
            if (value < tempNode.value) {
                tempNode = tempNode.left;
            } else if (value > tempNode.value) {
                tempNode = tempNode.right
            } else {
                break;
            }
        }

        return tempNode;
    }

    inOnder(node, arr = []) {
        if (node !== null) {
            if (node.left !== null) this.inOnder(node.left, arr);
            arr.push(node.value);
            if (node.right !== null) this.inOnder(node.right, arr);
        }

        return arr;
    }

    preOnder(node, arr = []) {
        if (node !== null) {
            arr.push(node.value);
            if (node.left !== null) this.inOnder(node.left, arr);
            if (node.right !== null) this.inOnder(node.right, arr);
        }

        return arr;
    }

    postOnder(node, arr = []) {
        if (node !== null) {
            if (node.left !== null) this.inOnder(node.left, arr);
            if (node.right !== null) this.inOnder(node.right, arr);
            arr.push(node.value);

        }

        return arr;
    }

}

Yukarıda yer alan Binary Search Tree Javascript kodunda ekleme, arama ve verileri sıralı bir şekilde ekrana basmanıza yarayacak fonksiyonlara yer verilmiştir.

Örnek Kod

let btree = new BinarySearchTree();

btree.insert(5);
btree.insert(12);
btree.insert(11);
btree.insert(55);
btree.insert(1);
btree.insert(5555);
btree.insert(3);

console.log(btree.find(5))
Girl Eating Pizza

If you're preparing for a senior Node.js developer position, it's important to be ready for technical interview questions that will test your knowledge of Node.js and its related technologies. In this

Girl Eating Pizza

As a JavaScript developer, it's essential to have a solid understanding of the language and its various concepts. In this article, we will provide some common interview questions and their answers to

Girl Eating Pizza

Frontend development is an essential part of web development that focuses on building the user interface of a website or application. Frontend developers are responsible for creating visually appealin

Girl Eating Pizza

Understanding how `this` works in JavaScript is essential for any developer working with the language. In this article, we will explore what `this` is, how it works, and common use cases.

Girl Eating Pizza

Event delegation is a concept in JavaScript that allows developers to handle events efficiently and improve the performance of web applications. In this article, we will explore what event delegation

Girl Eating Pizza

JavaScript is a powerful programming language that allows developers to create complex web applications. One of the most important concepts in JavaScript is the ability to handle asynchronous code. In

Girl Eating Pizza

As web applications become more complex, the size of the JavaScript bundles that are required to run them can become unwieldy. This can lead to slow load times and poor user experiences, particularly

Girl Eating Pizza

Managing state in a complex application can be a daunting task, but Redux can help simplify the process. Redux is a popular library for managing state in JavaScript applications, and it can be used wi

Girl Eating Pizza

React is a popular JavaScript library for building user interfaces. One of the key features that sets React apart from other libraries is its use of a virtual DOM. In this article, we will explore wha