




























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to binary search trees, explaining their terminology, implementation of the TreeSort algorithm, and the relationship between nodes and subtrees. It also covers the concept of a binary tree, the height of a tree, and the difference between complete and balanced trees. The document concludes with an overview of traversal methods and example implementations.
Typology: Study notes
1 / 68
This page cannot be seen from the preview
Don't miss anything!





























































A set of nodes (or vertices) with a single starting point ▪ called the root Each node is connected by an edge to another node A tree is a connected graph ▪ There is a path to every node in the tree ▪ A tree has one less edge than the number of nodes root node edge
yes! NO! All the nodes are not connected NO! There is an extra edge (5 nodes and 5 edges) yes! (but not a binary tree) yes! (it’s actually a similar graph to the blue one)
A leaf is a node with no children A path is a sequence of nodes v
… v
▪ where v i is a parent of v i+ (1 i n ) A subtree is any node in the tree along with all of its descendants A binary tree is a tree with at most two children per node ▪ The children are referred to as left and right ▪ We can also refer to left and right subtrees
C, E, F and G are leaves path from A to D to G subtree rooted at B
The height of a node v is the length of the longest path from v to a leaf ▪ The height of the tree is the height of the root The depth of a node v is the length of the path from v to the root ▪ This is also referred to as the level of a node Note that there is a slightly different formulation of the height of a tree ▪ Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root)
height of node B is 2 height of the tree is 3 depth of node E is 2 level 1 level 2 level 3
Each level doubles the number of nodes
1
2
Therefore a tree with h levels has 2 h +
the bottom level has 2 h nodes, that is, just over ½ the nodes are leaves
A binary tree is complete if ▪ The leaves are on at most two different levels, ▪ The second to bottom level is completely filled in and ▪ The leaves on the bottom level are as far to the left as possible
A traversal algorithm for a binary tree visits each node in the tree ▪ Typically, it will do something while visiting each node! Traversal algorithms are naturally recursive There are three traversal methods ▪ Inorder ▪ Preorder ▪ Postorder
The visit function would do whatever the purpose of the traversal is, for example print the data in the node