Download Binary Tree - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

B-tree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
AL-HUSEEN BIN TALAL UNIVERSITY
College of Engineering
Department of Computer Engineering
Algorithms and Data Structures
Recursion and Binary Tree
Course No.: 0511363
Fall 2014
Recursion
• Recursion: The process of solving a problem by
reducing it to smaller versions of itself
• Ex: The factorial of a natural number.
if n  0 
1
n ! 

n  (n  1)! if n  0
• This is written as n! and pronounced "n factorial"
Example
• The factorial function can be calculated by a
simple function based directly on the definition:
int fact(int n)
{
if n == 0
return 1;
else
return n * fact(n-1);
}
Ackermann function
int A(int m, int n)
{
if (m == 0)
return n+1;
else
if (n == 0 && m > 0)
return A(m-1, 1);
else
if (m > 0 && n > 0)
return A(m-1, A(m, n-1));
}
Fibonacci Sequence
• F=0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . .
• F(0)=0, F(1)=1, F(2)=1, F(3)=2,…….
• We can write F(n) as:
• Find F(5)?
• Fibonacci Sequence Function:
int f(unsigned n)
{
if(n < 2) return n;
// basis
else
return f(n – 1) + f(n – 2) ; // recursion
}
Smallest Element in the Array
#include<iostream.h>
int Min(int[] , int ) ;
main(){
const int size = 5 ;
int x[size] = {9,3,10,2,7};
cout<<" The minimum element is: "<<Min(x,size )<<endl;
}
int Min(int x[], int size)
{
int min ;
if(size==1) return x[size-1] ;
else
{
min = Min(x , size-1) ;
if(x[size-1] <= min) return x[size-1] ;
else
return min ;
}
}
Binary Tree
• A tree is a common hierarchical data structure used for many
computer science applications.
• Definition: A binary tree, T, is either empty or such that
I. T has a special node called the root node.
II. T has two sets of nodes, LT and RT, called the left
subtree and right subtree of T, respectively.
III. LT and RT are binary trees.
• Every node in a binary tree has at most two children
Binary Tree
• Example:
• In figure:
• (a), the root node is A, LA=empty, and RA= empty.
• (b), the root node is A, LA= {B}, and RA= empty, The
root node of LA = B, LB= empty, and RB= empty
• (c), the root :A, LA= empty, RA= {C}. The root of Rc=
C, Lc= empty, and Rc= empty.
Definitions
• A node in the binary tree is called a leaf if it has
no left and right children.
• The depth (Level) of a node A, in a binary tree is
the length of the path from A to the root of the
tree. Thus the root is at depth 0.
• The depth of the tree is equal to the deepest leaf
• The height of a nod A is the number of nodes on
the longest path from A to a leaf. Thus all leaves
are at height 0. If the binary tree is empty, the
height is 0.
• The height of the tree is equal to the height of
root.
Example
• The Depth of C is 1, the height
Of A is 2.
• The Depth of H is 3,height (H)
Is 0
• The depth of D is 2, the height
of D is 1
• The Depth of the Tree is
always equal to the height of the tree.
• If there is a path from B to D, B is called an
ancestor of D and D a descendant of B.
Implementation of Binary Trees
• To implement a tree, each node has its data and
two pointers one to each child of the node.
• The declaration of tree nodes
is similar in structure to that
for doubly linked list, in that
a node is a structure consisting of
the Information plus two pointers
(Left and Right) to other nodes.
Implementation of Binary Trees
#include<iostream.h>
#include<conio.h>
struct node {
int data;
node *left;
node *right;
};
node* insert(int x)
{
node* p = new node;
p->data = x;
p->left=NULL;
p->right=NULL;
return p;
}
Cont.
void setl(node* p,int x)
{
if(p==NULL)
cout<<"void insertion \n";
else if(p -> left !=NULL)
cout<<" invalid insertion \n";
else p ->left = insert(x);
cout<<" The inserted node is on the left of : "<<p->data<<endl;
}
void setr(node* p,int x)
{
if(p==NULL)
cout<<"void insertion \n";
else if(p -> right !=NULL)
cout<<" invalid insertion \n";
else p ->right = insert(x);
cout<<" The inserted node is on the right of : "<<p->data<<endl;
}
Height function
int height(node* tree)
{
if(tree == NULL)
return 0;
else
return 1+max(height(tree ->left),height(tree ->right));
}
int max(int &x,int &y)
{
if(x>=y)
return x;
else
return y;
}
Create binary Tree
main()
{
node* root;
node* p,q;
p=q=NULL;
int num =5;
root = insert(num);
setl(root,4);
p= root-> left;
setr(root,3);
q= root-> right;
setl(p,2);
setr(p,1);
setr(q,6);
}
Insertion Algorithm
void insertAlg(node* tree,int x)
{
node* p=tree; node* prev=0;
while(p!=0){
prev=p;
if (x> p->data)
p=p->right;
else
p=p->left;
}
if (tree==0)
tree=insert(x);
else if (x> prev->data)
prev->right=insert(x);
else
prev->left=insert(x);
}
Traversal of Binary Trees
• It is the moving through all the nodes of the
binary tree, visiting each node in turn.
• The key element in traversal orders is that to
decide if we are to visit the node itself before
traversing either subtrees or after traversing both
subtrees.
• In a traversal of a binary tree, each element of the
binary tree is visited exactly once.
• During the visit of an element, all action (make a
clone, display, evaluate the operator, etc.) with
respect to this element is taken.
Traversal of Binary Trees Ways
• There are 3 ways to
traverse that binary tree:
1. Preorder traversal: ABC
2. Inorder traversal: BAC
3. Postorder traversal: BCA.
Example
• Preorder: 12345
• Inorder: 14352
• Postorder:45321
Preorder Example
• Traverse this tree in Preorder way
• abdgheIcfj
a
b
f
e
d
g
c
h
i
j
Preorder Of Expression Tree
/
*
+
e
+
a
b
c
f
d
/ * + a b – c d + e f
Gives prefix form of expression!
Preorder Traversal Function
void preorder(node* tree)
{
if(tree!=NULL)
{
cout<<tree->data<<" ";
preorder(tree->left);
preorder(tree->right);
}
}
Inorder Example
a
b
f
e
d
g
c
h
i
j
g d h b e I a f j c
Inorder Example by projection
a
b
f
e
d
g
c
h
i
g d h b e I a
j
fjc
Inorder Of Expression Tree
/
*
+
e
+
a
f
b
c
d
a + b * c - d /
e + f
Gives infix form of expression (without parentheses)!
Inorder Traversal Function
void inorder(node* tree)
{
if(tree!=NULL)
{
inorder (tree->left);
cout<<tree->data<<" ";
inorder(tree->right);
}
}
Postorder Example
a
b
f
e
d
g
c
h
i
j
g h d I e b j f c a
Postorder Of Expression Tree
/
*
+
e
+
f
-
a
b
c
d
a b + c d - * e f + /
Gives postfix form of expression!
Postorder Traversal Function
void postorder(node* tree)
{
if(tree!=NULL)
{
postorder (tree->left);
postorder(tree->right);
cout<<tree->data<<" ";
}
}
Expression Trees
• The leaves of an expression tree are “operands”.
• The other nodes are operators.
• This particular tree happens to be binary, because all of operations
are binary.
• We evaluate an expression tree, by applying the operator at the root
to the values obtained recursively evaluating the left and right
subtrees.
Expression Tree construction
• Suppose the input is: ab+cde+**
• The first two symbols are operands, so create onenode tree and push pointers to them onto a stack.
• Next “+” is read, so two
pointers to trees are popped
and a new tree is formed.
• A pointer to the tree is pushed
onto the stack.
Cont.
• Next, c, d and e are read and a for each a onenode tree is created and pushed onto the stack.
• Next a “+” is read, the top two
most tree pointers are popped
and a new tree is formed.
• A pointer to the new tree is
pushed onto the stack.
Cont.
• Next, “*” is read the two pointers on top of the
stack are popped and a new tree is formed.
• A pointer for the new tree is pushed onto the
stack.
Cont.
• Next, a “*” is read the top two pointers are
popped and new is formed.
• Its pointer is pushed onto the stack.