A rooted tree is called an m-ary tree if every internal vertex has no more than m children.
A rooted tree is called a full m-ary tree if every internal vertex has exactly m children.
Binary Trees
A rooted tree is called a full binary tree if every internal vertex has exactly 2 children: a left child and a right child
graph G { label="full binary tree" node[shape="point"] 1--2,3 2--4,5 5--6,7}
When we have a full binary tree of height n:
the left and right subtrees of the root are both full binary trees of height ≤n−1
at least one of the left and right subtrees of the root is a full binary tree of height n−1
Counting vertices and edges of trees
A full binary tree with n internal vertices contains 2n−1 vertices altogether.
Every vertex, except the root, is the child of an internal vertex.
Because each of the n internal vertices has 2 children, there are 2n vertices in the tree other than the root.
A full m-ary tree with n internal vertices contains m⋅n+1 vertices altogether.
Every vertex, except the root, is the child of an internal vertex.
Because each of the n internal vertices has m children, there are m⋅n vertices in the tree other than the root.
Searching for items in a linearly ordered list is important and Binary search trees are particularly useful in representing elements in such a list. There are very efficient methods for:
searching for data in binary search trees
revising data in binary search trees
converting linaerly ordered lists to binary search trees and back
title: Example: lexicographical order on wordsFirst, we order the letters of the English alphabet as usual:$$ a \prec b \prec c \prec d \prec e \prec ... \prec x \prec y \prec z$$Then, we can use this ordering of the letters to order longer words:- Given two words $w_1$ and $w_2$, we compare them letter by letter, from left to right, passing equal letters.- If at any point a letter in $w_1$ is $\prec$-smaller than the corresponding letter in $w_2$, then we put $w_1 \prec w_2$.- If every letter in $w_1$ is equal to the corresponding letter in $w_2$ but $w_2$ is longer than $w_1$, then we also put $w_1 \prec w_2$.- In any other case, we put $w_2 \prec w_1$.$$ \text{discreet} \prec \text{discreetness} \prec \text{discrete} \prec \text{discretion} \prec \text{geography}$$$$ \prec \text{geology} \prec \text{mathematics} \prec \text{physics} \prec \text{psychology} $$
Given two things: a list of L items and a linear order ≺ on them.
A binary search tree for L and ≺ is a binary tree in which every vertex is labelled with an item from L such that we have:
the label of each vertex:
is ≺-greater than the labels of all vertices in the left subtree
is ≺-less than the labels of all vertices in its right subtree
every path in the tree be compatible with the order of listing:
We are given a list L of items, and a linear order ≺ on them.
We go through each member of the list, from left to right:
First item: becomes our root
Comparing: We take the next item in the list, and first compare it with the labels of the ‘old’ vertices already in the list, starting from the root then:
moving to the left: if the new item is ≺-less than the label of the respective ‘old’ vertex, if this ‘old’ vertex has a left child, or:
moving to the right: if the new item is ≺-greater than the label of the respective ‘old’ vertex, if this ‘old’ vertex has a right child.
Adding:
When the new item is ≺-less than the label of an ‘old’ vertex and the vertex has no left child, insert a new left child to the ‘old’ vertex and label it with the new item.
When the new item is ≺-larger than the label of an ‘old’ vertex and the vertex has no child, then we insert a new right child to the ‘old’ vertex and label it with the new item.
Example BST
Build a binary search tree for the following list of words using the lexicographical order ≺.
Finally, we take psychology, compare it with mathematics, move to the right, compare it with physics, move to the right then compare it with zoology, psychology≺zoology, hence create a new left child:
Say we had to add a new word, chemistry, to our BST.
How many comparisons do we need to locate or find it?
In this case, it’s just 2. We compare it with mathematics, move left, compare it with geography.
As chemistry≺geography and geography has no left child, we know that chemistry is not in the tree.
We can create a new left child and label it as such.
Rooted trees are often used to store information. We use different systematic approaches to visit each vertex of a rooted tree to access data, called traversal algorithms.
Common traversal algorithms:
Preorder traversal: visit the root, continue traversing subtrees in preorder, from left to right
Inorder traversal: begin traversing leftmost subtree inorder, then visit root, then continue traversing subtrees in inorder, from left to right
Postorder traversal: begin traversing leftmost subtree in postorder, then continue traversing subtrees in postorder, from left to right, finally visit root