Binary treeIn computer science, a binary tree is a type of tree (data structure) where each item within the tree has at most two children. Types of binary trees![]()
RepresentationsArrayA binary tree can be implemented using an array by storing its level-order traversal.[1] In a zero-indexed array, the root is often stored at index 1. For the nth item of the array its:
ReferencesIn a programming language with references, binary trees are typically constructed by having a tree structure which contains references to its left child and its right child. Traversals![]() Pre-orderThe current item is visited, then the left branch is visited, and then the right branch is visited. void preOrder(Item item) {
if (item == null) return;
visit(item);
preOrder(item.left);
preOrder(item.right);
}
In-orderThe left branch is visited, then the current item is visited, and then the right branch is visited. void inOrder(Item item) {
if (item == null) return;
inOrder(item.left);
visit(item);
inOrder(item.right);
}
Post-orderThe left branch is visited, the right branch is visited, and then the current item is visited. void postOrder(Item item) {
if (item == null) return;
postOrder(item.left);
postOrder(item.right);
visit(item);
}
References
|
Portal di Ensiklopedia Dunia