Professional Documents
Culture Documents
TREE
Dr
Mourad Raafat
Definition
v is a descendant of a node u if u is an
ancestor of v.
pr3 is a descendant of cs016/.
Complete BT
every level complete
May be complete left side
Perfect BT
All nodes have two children except leaves also same level
A binary tree representing an arithmetic expression. This tree represents the expression ((((3 +1)×3)/((9
−5)+2))−((3×(7−4)) +6)). The value associated with the internal node labeled “/” is 2.
Motivation for BT
1- If we have to represent the data in a linked structure, i.e., linked list, (because we
cannot anticipate the maximum number of elements), while keeping the search fast.
Notice that: this idea is immediate after our analysis to the binary search in arrays.
2- Building Expression Trees (has great importance in syntactical analysis and parsing),
along with the validity of expressions
+ log !
a b b n
a+b log b n!
a
b c
a+bc 12
Before ADT and coding, How to traverse a BT? More motivations and benefits
There was no problem for traversing a linear structure like a list. For a BT, at each node
V having left and right subtrees L and R (respectively) we can do the following visiting:
The standards are Pre, In, and Post order. In the three of them, L precedes R; then V is
before them (Pre) or in between (In) or after them (Post).
Example: +
The Polish Forms (Ch 12) are
Preorder: +ab related to these orders.
Inorder: a+b b Preorder Prefix
a
Postorder: ab+ Inorder Infix
a+b Postorder Postfix
+
Example:
Also notice the strong
Preorder: +abc a connection between these
Inorder: a+bc traversal modes on a hand
Postorder: abc+ and recursion and stacks on
b c
the other hand. 13
a+bc
Before ADT and coding, How to traverse a BT? More motivations and benefits
Notice that:
For a BT produced by binary search, the Inorder traversal produces sorted elements.
5
2 8
1 3 6 9
F F F 4 F 7 F 10
F F F F F F
1 2 3 4 5 6 7 8 9 10
Now, we have enough motivation for having BTs as data structures. Let us define
the ADT and start coding as linked implementation.
14
Definition: A Binary Tree ADT is either empty, or it consists of a node (vertex)
called the root together with two binary trees called the left subtree and the right
subtree of the root. This is together with the following operations:
We will define the next operations for a special type of BT, i.e., Binary Search
Trees (which will be defined later).
7. Insert a new entry (we have to define where)
8. Delete an entry (we have to define from where)
9. Search for an element.
10. (Any other operation to be defined later).
15
/*Tree.h*/ Linked Implementation
#include "Global.h“
16
void CreateTree(Tree *pt){ pt t
*pt=NULL;
}
entry
}
17
//Pre: Tree has been created and intialized.
pt t
//Post: Inorder traversal.
void InorderRec(Tree *pt, void(*pvisit)(TreeEntry)){
if (*pt){ entry
InorderRec(&(*pt)->left, pvisit);
(*pvisit)((*pt)->entry);
InorderRec(&(*pt)->right, pvisit); entry
}
}
Notice that, we pass a pointer to the tree to entry
be consistent with other functions.
//User level
Tree t;
InorderRec(&t, &Display)
18
How to write the previous function iteratively?
void Inorder(Tree *pt, void (* pvisit)(TreeEntry)){
Stack s;
void *p=(void *)(*pt);
/* p will be Pushed in the stack; we do not define it TreeNode *
to avoid modifying Stack.h to include Tree.h*/
if(p){ pt t
p
CreateStack(&s);
do{
while(p){ entry
Push(p, &s);
p=(void *)(((TreeNode *)p)->left); entry
}
if(!StackEmpty(&s)){
//This is redundant check; always 1 (prove) entry
Pop(&p, &s);
(*pvisit) (((TreeNode *)p)->entry);
p=(void *)(((TreeNode *)p)->right); pt
}
}while(!StackEmpty(&s) || p); T
} root size depth
}
void Inorder2(Tree2 *pt, void (* pvisit)(TreeEntry)){
void *p=(void *)(pt->root);//The only change 19
}
pt t
20
void ClearTreeRec(Tree *pt){ pt t
if (*pt){
ClearTreeRec(&(*pt)->left);
ClearTreeRec(&(*pt)->right); entry
free(*pt);
*pt=NULL;
} entry
}
void ClearTreeRecAux(Tree *pt){
if (*pt){ entry
ClearTreeRecAux(&(*pt)->left);
ClearTreeRecAux(&(*pt)->right);
free(*pt);
}
}//of course, this is not provided for the user.
It is very smart to notice that (as was noticed by some students during the lecture)
ClearTreeRecAux is faster than ClearTreeRec because it saves the step *pt=NULL in
each recursive call. However it does not set the tree to NULL at the end. We can rewrite a faster
version of ClearTreeRec as: