Beruflich Dokumente
Kultur Dokumente
Trees
Tree is a non linear data structure & is mainly used to represent data containing a hierarchical relationship between elements.
Binary Tree
Binary Tree is defined as a finite set of elements called nodes, such that
T is empty (called the null tree or empty tree) T contains a distinguished node R (root) and the remaining nodes of T form an ordered pair of disjoint binary trees T1 & T2.
Edge a line drawn from a node N of T to a successor is called an edge. Path a sequence of consecutive edges.
Each node in a binary tree T is assigned a level no. The root R of a tree T is assigned the level no 0. Same level nodes are said to belong to the same generation
Depth (Height) of a tree T is the maximum number of nodes in a branch of T. This turns out to be one more than the largest level number of T. Tree with only one node (the root) has a height of one.
Trees Terminology
Root no parent Leaf no child Interior non-leaf Height maximum number of nodes in a branch of T
Root node
Interior nodes
Leaf nodes
Array Representation is mostly suitable for complete binary tree, but for other, there are wastage of memory.
2
a
NLR
2
a
Inorder:
1. Traverse the left subtree of N in Inorder. 2. Process the root N. 3. Traverse the right subtree of N in Inorder.
LNR
2
a
Postorder:
1. Traverse the left subtree of N in Postorder. 2. Traverse the right subtree of N in Postorder 3. Process the root N
LRN
2
a
Example
X>Y X<Z
10
2 5 2 25 30 45 5
10 45
30
45 10
2 25
25
30
(b) Repeat step (a) until one of the following occurs (I) A node such that ITEM = N (successful search) (II) An empty subtree is found, indicating unsuccessful search. Then we insert ITEM in place of empty subtree.
Program
Struct Node{ int info; Node* left; Node* right; }
Insert
Node* Insert(Node* tree, int item) { if(tree==NULL){ tree =(Node*) malloc(sizeof(Node)); tree->left = tree->right = NULL; tree->info = item; }
Insert
else if(item < tree->info) tree->left= Insert(tree->left, item);
Insert
else { printf(Duplicate Node\n); exit;}
return(tree); }
Search
Void Search(Node* tree, int item) { if(tree==NULL) printf(Tree is empty\n); else if(item == tree->info) printf(The item is %d \n, item);
Search
else if ( item < tree->info) Search(tree->left, item);
Preorder
void Preorder(NodeT* tree) { if(tree!=NULL){ printf(%d\n,tree->info); Preorder(tree->left); Preorder(tree->right); }
Inorder
void Inorder(NodeT* tree) { if(tree!= NULL){ Inorder(tree->left); printf(%d\n, tree->info); Inorder(tree->right); }
Postorder
void Postorder(NodeT* tree){ if(tree!=NULL) { Postorder(tree->left); Postorder(tree->right); printf(%d\n,tree->info); } }
Deletion
There are three possible cases to consider: Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree. Deleting a node with one child: Remove the node and replace it with its child.
Deletion
Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Replace the value of N with the value of R, then delete R.
Deletion
As with all binary trees, a node's in-order successor is the left-most child of its right subtree, and a node's in-order predecessor is the right-most child of its left subtree. In either case, this node will have zero or one children. Delete it according to one of the two simpler cases above.
Deletion
NodeT* findSuc(NodeT * tree, NodeT* p) { if(tree == NULL) { printf("Empty tree\n"); return tree; } else { NodeT* sucPtr = p->right; while(sucPtr->left != NULL) sucPtr = sucPtr->left; return sucPtr; } }
return tree;