Tree
Tree
Tree
• Root node: The root node R is the topmost node in the tree.
If R = NULL, then it means the tree is empty.
• Sub-trees: If the root node R is not NULL, then the trees T1 , T2 , and
T3 are called the sub-trees of R.
• Leaf node: A node that has no children is called the leaf node or the
terminal node. Path A sequence of consecutive edges is called a path.
For example, the path from the root node A to node I is given as, I leaf
node.
• Ancestor node: An ancestor of a node is any predecessor node on the
path from root to that node. The root node does not have any
ancestors. In the tree given in previous slide nodes A, C, and G are the
ancestors of node K.
Basic Terminology
1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees
General Trees
• A binary tree of height h has at least h nodes and at most 2h – 1 nodes. This
is because every level will have at least one node and can have at most 2
nodes.
• So, if every level has two nodes then a tree with height h will have at the
most 2h – 1 nodes as at level 0, there is only one element called the root.
• The height of a binary tree with n nodes is at least log2 (n+1) and at most n.
• In-degree/out-degree of a node: It is the number of edges arriving at a
node. The root node is the only node that has an in-degree equal to zero.
Similarly, out-degree of a node is the number of edges leaving that node.
• Binary trees are commonly used to implement binary search trees,
expression trees, tournament trees, and binary heaps.
Complete Binary Trees
• A binary tree is a structure of nodes of the same type. Each node is the root
of a sub-tree, having a left and right child which are binary trees. Each node
has a key field that identifies the entry.
• Operations:
init_t(t) – initialize an empty tree.
empty_t(t) – boolean function to return true if the tree t is empty.
height(t) – function to return the height of tree t.
make_root(n) – procedure to make node n the root of a tree.
addleft(t, n) – add node n as a left child of tree t.
addright(t, n) – add node n as a right child of tree t. d
deleteleaf(n) – delete the leaf node n.
Binary tree
typedef struct nt
{ T info;
struct nt *left, *right;
} bintree;
bintree *t, *cur;
bintree *init t() { return NULL; }
int empty_t(bintree *t) { return (t == NULL) ;}
bintree *makeroot(T n) {
bintree *t;
if((t = malloc(sizeof(bintree))) == NULL)
print error(“malloc error”);
else {
t -> info = n;
t -> left =NULL;
t -> right = NULL; }
return t; }
Binary tree
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way. Unlike linear data structures in
which the elements are traversed sequentially, tree is a nonlinear data
structure in which the elements can be traversed in many different
ways. There are different algorithms for tree traversals. These
algorithms differ in the order in which the nodes are visited.
Pre-order Traversal
• The left subtree of a node contains only nodes with keys lesser than the node’s
key.
• The right subtree of a node contains only nodes with keys greater than the
node’s key.
• The left and right subtree each must also be a binary search tree.
• BST) is a data structure that is commonly used to implement efficient searching,
insertion, and deletion operations.
Insert a value in a Binary Search Tree:
• Check the value to be inserted (say X) with the value of the current
node (say val):
• If X is less than val move to the left subtree.
• Otherwise, move to the right subtree.
• Once the leaf node is reached, insert X to its right or left based on the
relation between X and the leaf node’s value.
Algorithm to search for a key in a given Binary Search Tree:
A multiway tree is a tree that can have more than two children. A multiway tree of order
m (or an m-way tree) is one in which a tree can have m children. As with the other trees
that have been studied, the nodes in an m-way tree will be made up of key fields, in this
case m-1 key fields, and pointers to children.
Here M = 3. So a node can store a maximum of two key values and can contain pointers to
three sub-trees. In our example, we have taken a very small value of M so that the concept
becomes easier for the reader, but in practice, M is usually very large. M-way search tree of
order 3
B tree
• B Tree is a specialized m-way tree that can be widely used for disk
access. A B-Tree of order m can have at most m-1 keys and m
children. One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key values by
keeping the height of the tree relatively small.
B tree properties
Insert 39
Deleting an Element from a B Tree