Resource Person: Zafar Mehmood Khattak
Resource Person: Zafar Mehmood Khattak
Types of Trees
Binary Tree
Full Binary Tree
Extended Binary Tree
Complete Binary Tree
Each object of a tree starts with a root and extends into several
branches
Each branch may extend into other branches until finally terminated
by a leaf
Each tree has one node designated as the root of the tree
A unique path exists from the root node to all other nodes in the tree
Each node, other that the root, has a unique predecessor, it is also
called the parent
Each node may have no, one or several successor nodes, it is also
called the child
General characteristics of trees
Node
Degree
Children
Parent
Siblings
Ancestors
Leaves
Path from node n1 to nk
a sequence of nodes n1,n2,...,nk such that ni is the parent of ni+1 for 1<= i < k
Path length
Level
Height of a node
Height of a tree
Depth of a node
Depth of a tree
Along with arrays and lists, trees are the most frequently encountered data
structure in computer algorithms
Basic terms of Trees
Root Node
The unique node in the tree that has no parent node
Parent Node
The node, which generates links for other nodes
Child Node
The node that is directly connected to a parent node
Subtrees
The child node of the root node that has its own child nodes
Basic terms of Trees
Terminal or leaf Node
The node having no successor or child
Siblings
The nodes having a common parents
Depth of Node
Each node has a unique path connecting it to the root. The depth of a node is
the total number of nodes between the node and root
Height of Tree
The maximum depth among all of the nodes of a tree
Basic terms of Trees
Empty Tree
A tree that has no node
Degree of Node
The number of children of a node
Full Tree
A tree, which all its internal nodes have the same degree and all the
leaves are at the same level
Singleton Tree
The tree whose root is its only node
Basic terms of Trees
Root Node
The node that has no parent
16
19
15
66
9
8 12 61
Operation on binary tree
Insertion
Deletion
Searching
Traversing
1. check, whether value in current node and a new value are equal. If
so,
1.1. duplicate is found.
1.2. And exit, Otherwise,
2. if a new value is less, than the node's value:
2.1. if a current node has no left child,
2.2 place for insertion has been found;
2.3. otherwise, handle the left child with the same algorithm.
3. if a new value is greater, than the node's value:
3.1. if a current node has no right child,
3.2. place for insertion has been found;
3.3. otherwise, handle the right child with the same algorithm.
Insertion code.
.
infix expression
a+b*c+d*e+f*g
Preorder Traversal (NLR: node, left,
right)
algorithm preorder(Treeptr T);
(T is a pointer to a node in a binary tree. For full tree
traversal, pass preorder the pointer to the top of the
tree)
begin
if T != NULL
then
write(T->data);
preorder(T->Lchild);
preorder(T->Rchild); Preorder
endif
Output: ABDFEGCH
PostOrder Traversal (LRN: left, right,
node)
algorithm postorder(Treeptr T);
(T is a pointer to a node in a binary tree. For full
tree traversal, pass postorder the pointer to the top
of the tree)
begin
if T != NULL
then
postorder(T->Lchild);
postorder(T->Rchild);
write(T->data);
endif
Output: FDGEBHCA
Postorder traversal, left, right, node.
.
postfix expression
abc*+de*f+g*+
Traversing code
E= ( a + b * c ) + (( d * e + f) * )
Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25