Skip to content

Latest commit

 

History

History
140 lines (103 loc) · 2.19 KB

tree.md

File metadata and controls

140 lines (103 loc) · 2.19 KB
theme transition title enableMenu enableSearch enableChalkboard controls slideNumber hashOneBasedIndex
white
slide
Árvores e o DOM
true
false
true
true
true
false

Árvores

Luís Antônio (tonhao.dev)


Árvores na teoria da computação

-


Alguns nomes

-


Habilidade importante

-


Fibonacci

function fib(n) {
    if (n <= 2) return 1;
    return fib(n - 1) + fib(n - 2);
}

fib(6)

Fibonacci Tree

-


Alvo

-


Node

class Node {
    constructor(value) {
        this.value = value;
        this.children = [];
    }
}

Tree - Constructor

class Tree {
    constructor(root) {
        this.root = root;
    }
}

Tree - Insert

class Tree {
    ...
    insert(key, node) {
        this.insertNode(this.root, key, node);
    }
    ...
}

Tree - Insert Node

class Tree {
    ...
    insertNode(currentNode, key, newNode) {
        if (currentNode.value === key) {
            currentNode.children.push(newNode);
            return;
        }

        for (const node of currentNode.children) {
            this.insertNode(node, key, newNode);
        }
    }
    ...
}

Árvore do exemplo

-


Recriando no código

const tree = new Tree(new Node(1))
tree.insert(1, new Node(2))
tree.insert(1, new Node(3))

tree.insert(2, new Node(4))
tree.insert(2, new Node(5))
tree.insert(2, new Node(6))

tree.insert(5, new Node(7))
tree.insert(5, new Node(8))

tree.insert(3, new Node(9))

Output

-