The dendritic trees of neurons play an important role in the information processing in the brain. Although it is widely accepted that dendrites require independent compartments to perform most of their computational functions, it is still not understood how they compartmentalize into functional subunits. I will show how these subunits can be deduced from the structural and electrical properties of dendrites through a mathematical formalism that links the dendritic arborization to an impedance-based tree-graph. I will show how the topology of this tree-graph reveals independent dendritic compartments. I will also show that balanced inputs and shunting inhibition lead to a modification of this topology and hence reconfigure the number and size of compartments in a context-dependent, temporal manner. Finally, I will show that this dynamic recompartmentalization enables branch-specific learning of stimulus features in branch-pairs where this would have been impossible at rest.