liveBook · Manning

Binary tree

As explained in Learn C++ by Example

A binary tree is a tree data structure in which each node has at most two child branches.

A binary tree stores elements in nodes, with each node pointing to at most two child nodes. A binary search tree orders smaller elements to the left and larger elements to the right. section 7.2.1

The C++ map uses a binary search tree to store key-value pairs. The key determines whether each pair moves left or right, making searches faster than scanning every element in a vector. section 7.2.1

Following a search

Insert {1:a}, {3:c}, and {5:e}. The tree is reorganized so {3:c} becomes the top node, with {1:a} on the left and {5:e} on the right. Searching for {2:b} compares against 3, moves left to 1, and stops at that leaf. section 7.2.1

Figure 7.2 Three elements in a balanced binary search tree: nodes with smaller keys go on the left and larger keys on the right.
CH07_F02_Buontempo

Use the balanced-tree figure to see how key ordering places smaller nodes left and larger nodes right. section 7.2.1

How Learn C++ by Example covers it

How deep it goes
Explains nodes, child pointers, ordering, balancing, and one search path, but stops short of implementation code.
Examples
Uses map pairs {1:a}, {3:c}, and {5:e}, plus a search for {2:b}, to demonstrate insertion and traversal.
What it assumes
Assumes familiarity with vectors, arrays, and map key-value pairs while introducing nodes, child pointers, leaves, and ordering.
What you can do after
Evaluate how map ordering and balancing reduce search work compared with linear container scans.
How it connects
Connects binary trees with maps, vectors, arrays, binary search trees, keys, values, and time complexity.

Also explained in

Compare how 4 books explain binary tree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.