Professional Documents
Culture Documents
Intro To Trees-01
Intro To Trees-01
Example
A
B C
D E F
G H I
This tree consists of nine nodes with A as its root. Its left subtree
is rooted at B and its right subtree is rooted at C. The binary trees
rooted at D,G,H and I have empty right and left subtrees.
A node that has no children nodes is called a leaf node.
Node n1 is an ancestor of node n2 if n1 is either the father of n2
or the father of some ancestor of n2.For example A is the ancestor
of G, and H is a descendant of C but E is neither an ancestor nor
a descendant of C.
A node n2 is left descendant of node n1 if n2 is either the left son
of n1 or a descendant of the left son of n1.
Level of a node
Example
A
B C
D E F
G H I
Strictly binary Tree??
No. why?
A
B C
D E F
G H I
Strictly binary Tree??
B F
D E
Strictly binary Tree??
Yes
A
B F
D E
Strictly binary tree
Examples
A
B C
D E
Complete binary tree??
No.
A
B C
D E
Complete binary Tree
Example
A
B C
D E H F
Types of Binary Trees
Degenerat Complete
e binary binary
Almost complete binary tree
Inorder
Preorder
postorder
Traversal
Inorder
Traverse left subtree
Visit root node
Traverse right subtree
Traversal
Preorder
Visit root node
Traverse left subtree
Traverse right subtree
Traversal
Postorder
Traverse left subtree
Traverse right subtree
Visit root node
Example 1
int data
struct mytree *left,*right; left right
}tree;
tree *root=NULL;
tree * create(tree *root, int data)
{ if (root==NULL)
{
root=(tree *)malloc(size of (tree));
root->left=root->right=NULL;
root->data=data;
}
else if(data<root->data)
{ root->left=create(root->left,data); }
else if(data>root->data)
{ root->right=create(root->right,data); }
else if(data==root->data)
{ printf(“Redundancy Error, Cannot Insert”); }
return root;
}
BST Search Implementation