Tree

Example (1): The node representing the university is the parent and a node representing a college is a child. In the same example the IT college is the parent of the CS ,CE and BIS departments.

 

 

Example(2):Drives in a harddisk and the folders, subfolders and files.

 

Example: Mathematical expression.

 

 

The content of a tree can be traversed in 3 different forms to present the expression in infix ,postfix and prefix forms.

traversal form

operations

expression

example

pre-order

(N,L,R) The root is visited first followed by the left then the rights nodes. (N=Node , L=Left , R=Right)

prefix

*+324

In-order  

(L,N,R) The left node is visited , followed by the root , and then right .

infix

32+4*

Post-order

(L,R,N) The left node is visited , followed by the right and then the root.

postfix

3+2*4

Example: store data for fast retrieval using binary search tree.

case

searches

Best case

Log N

worst case

N ( it will take time equal to linear search )

 

For example , 3 comparisons are needed to locate 'Yousif' in the first figure , but in the second figure  , 7 comparisons are necessary.

Example : The tree rooted at 'Mariam'  and 'Tariq' are examples of balanced trees. ( See the next figure)

 

Operations on binary search tree:

  1. Create a tree ( Constructor)

  2. Destroy a tree (Destructor)

  3. Attach left and right (insertion)

  4. Detach left and right (deletion)

  5. Pre- , in - , post- order traversal

  6. Set and get node details.

Pointer-Based Representation (simpler)

 

Data declaration

Typedef struct TreeNode * ptrtype;

Struct TreeNode{

                                int data;

                                PtrType left;

                                PtrType right ;

                               }

ptrtype root;

 

Example:

Search

Looking for Bedoor , we first check the root node , and Since Bedoor is less than 'Maryam' we branch to the left of the tree .Compare with 'Fatima' and branch left again , compare with 'Bader' and branch right to locate 'Bedoor'.

Insertion

Example : We want to insert a record for 'Salma' . Since 'Salma' is greater than 'Maryam' ,less than 'Tariq' and greater than 'Mazin' and Mohammed' , 'Salma' will be on the right of 'Mohammed'.

Deletion

Node being deleted

Operation

is a leaf node

Initialize its parent's pointer that points to the node to be deleted to NULL.

has only one child

The parent's pointer is reset to point to the child of the node to be deleted whether left or right .

has 2 children

We have 2 choices: (1) The item whose search key is immediately greater in value than the deleted node's search key (in-order successor)  OR  (2) immediately less than it (in-order predecessor ). These are the left most node of the deleted node's right subtree , and the right most node of its left subtree. These nodes can either be leaf or have one child  and their deletion is easier.

In order traversal : Ali , Bader , Bedoor , Fatima , Maisa , Maryam , Mazen , Mohammed , Tariq , Yousif.

in-order predecessor : Maisa

in-order successor : Mazen

Retrieval

    Same search

Traversal

    Best done recursively.

Preorder(T)

if ( T is not empty)

   { visit the current node

     Preorder(left subtree of T)

     Preorder(right subtree of T)  }

Inorder(T)

if ( T is not empty)

   { Inorder(left subtree of T)

     visit the current node

     Inorder(right subtree of T) }

Postorder(T)

if ( T is not empty)

   { Postorder(left subtree of T)

      Postorder(right subtree of T)

      visit the current node }

 

Array -based Representation

 

Data declaration

 struct TreeNode{

                 int data;

                 int left;

                 inr right;}

int root,free;

TreeNode tree[100];

The root is placed in position 1 , then for any element in position i , its left child can be found in position 2i and its right child resides in  2i+1 . The parent can be found at position i/2.

  A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10

Complete Binary Tree and its Array Based Representation