Trees
Last updated
Last updated
An absctract data type that represents a hierarchical tree structure with a set of connected nodes.
Useful fo represent data in a non-linear form, making them suitable for representing hierarquical relationships.
Each node in the tree can be connected to many children nodes (depending on the type of tree), but must be connected to exactly one parent node.
You may assume that in a Tree with V
number of vertices (nodes), it will have amount of E
edges. (Because each node will have at least 1 edge, connecting it to it's parent node)
These constraints mean:
There are no cycles or "loops". (no node can be its own ancestor)
Each child can be treated like the root node of its own subtree. (Making recursion
a useful technique for tree traversal)
node
The fundamental unit of the tree that holds data.
edge
The connection between two nodes.
root
The topmost node. (Has depth 0)
leaf
Node without children.
height
The longest path from the root to the deepest leaf.
degree
For a given node, its the number of children. (A leaf has degree of 0)
depth
The number of edges from the root to the node.
level
The same as depth, but it starts the count from 1, instead of 0.
There are 3 types of tree traversals. (That depends on when you do the visiting of the node)
Pre-Order traversal. (Visit the node, then go Left, then go Right)
In-Order traversal. (Go Left, then visit the node, then go Right)
Post-Order traversal. (Go Left, then go Right, then visit the node)
Trees where a node may have any amount of children.
A tree type in which each node has at most two children. (It may have 0, 1 or 2)
A sub-type where every node has either 0 or 2 children.
All levels, except possibly the last, are fully filled.
All nodes are as far left as possible.
All internal nodes have two children.
All leaves are at the same level.
A Binary Tree in which:
The left child contains values smaller than the parent.
The right child contains values greater than the parent.
Just look for the place that satisfy BST conditions and add.
Will eventually unbalance the tree.
Deleting is bit more complicated and have 3 separate cases to look for:
Having a height
property on each node, helps choosing in Case 3 which child has the highest height
.
And choosing the child with the highest helps pruning the tree and reducing its height
.
A Binary Tree where the height difference between the left and right subtrees of every node is at most 1.
A self-balancing Binary Search Tree
where the height of the left and right subtrees differ by at most 1.
Is more aggressive in maintaining the Tree balanced, since it may have more rotations and more complex rotations. (Compared to the Red-Black Tree
)
Preferred in specific cases, where the aggressive balance maintainence is required.
Has a better search time than Red-Black Trees
.
It is a self-balancing Binary Search Tree
with specific coloring rules to maintain balance.
Usually for most cases Red-Black Trees
are preferred over AVL Trees
.
The color
field can be just a 1bit property like 0
or 1
.
The root node is always Black.
By default every new node in the Tree will be Red. Then it wil be checked to see if it can keep being Red or if it will be changed to Black.
A Red node can only have Black parents and children.
Have a better insertion time than AVL Trees
. (Since it is less aggressive in balancing the Tree)
A General Tree
specifically used for efficient retrieval of strings, commonly used in autocomplete features.
Each node can have any number of children.
Starting a the root of Trie.
Grab one character of the string being added. (From left to right, in most Languages)
Check if the current node has this character.
If it doesn't add the new node with this character.
Set the current node to this node and Repeat 2.
The last node which refers to the last character of the word being added, can have a flag like isWord
to inform that this node completes a word on the Trie
.
Recursevely walk the Tree until the end of the deleted string.
Delete the nodes on the Post Section
of the Recursion, that have only one child.
In some scenarios no nodes will be deleted only the isWord
will be set to false
.
A tree used for efficient prefix sums and range queries.
Self-balancing Search Trees.
B-Trees can have N
children.
Self-balancing Search Trees designed for filesystems and database systems.
It is a Binary Tree
where every child is:
Smaller than the current node. (Max Heap
)
Larger than the current node. (Min Heap
)
Heap
has a weak ordering, meaning that it is not totally ordered like a BST
.
The order is only locally maintained, enough to satisfy the Heap
conditions above.
Heap
is usually a Full Binary Tree or Complete Binary Tree.
Whenever a node is added or deleted, the Tree MUST be ajusted.
Used in priority queues for example.
Nodes of the Heap
follow a sequence to defines the first
and last
node of the Tree.
This sequence also helps on insertion
and deletion
, since inserted elements are added to the end (or last position), and deleted elements are deleted at position 0 (first position).
Elements are added at either:
The last level (height) where you have an empty space. (Usually from left to right)
New created level. (Usually from left to right)
Repeat 2. until Heap
conditions are satisfied.
The repeat 2. process is called Heapify Up, where the added node thies to bubble up in the Tree.
The top element is the one deleted.
Repeat 3. until Heap
conditions are satisfied.
The repeat 3. process is called Heapify Down, where the node that took place of the deleted node tries to bubble down in the Tree.
Heaps can easily be implemented with Arrays
Data Structures, and thus facilitating the removal of nodes.
You simulate left
and right
childs and parent
nodes in Arrays with the following formulas: (where i
is the current node index being looked)
Runs on up to in the worst case, depending on how the Tree structure is.
Case 1: The deleted node has no children, so just points to undefined
.
Case 2: The deleted node has only 1 child, so points to child.
Case 3: The deleted node has both children, so will point to either:
The largest decendent node of . (largest on small side)
The smallest decendent node of . (smallest on large side)
Then check the added element against to see if it satisfy Heap
conditions.
Runs on since the Tree is always balanced.
Then the very last node in the Tree takes the place of the top node.
The node is checked against:
In a MinHeap
, the smallest of , then if they swapp positions.
In a MaxHeap
, the highest of , then if they swapp positions.
Runs on since the Tree is always balanced.
left child
:
right child
:
parent node
: