ADT Tree. Tree : nonempty collection of vertices and edges that satisfies certain requirements. Vertex ( node ): Have a name and carry other associated information Edge : A connection between two vertices.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
ADT Tree
Tree: nonempty collection of vertices and edges that satisfies certain requirements.
Vertex (node): Have a name and carry other associated information
Edge: A connection between two vertices.
We usually draw trees with the root (node A)at the top and leaves at the bottom!
Subtree
Subtree
Binary tree : no node can have more than two children (left child and right child).
+
C
D
_
A
B
E
AB+ CD/E - which is the postfix of (A + B) ((C / D) – E).
Data Structure:
typedef struct Node *Node_ptr;
struct Node
{
ElementType element;
Node_ptr left;
Node_ptr right;
} ;
typedef Node_ptr tree;
Inorder:
Preorder:
Postorder:
Inorder : B A C
Preorder : A B C
Postorder : B C A
A
B
C
Inorder: D B E A F C G
Preorder: A B D EC F G
Postorder: D E BF G C A
A
C
B
G
F
D
E
Inorder :
G D H B A E I C J F K
Preorder:
A B D G HC E I F J K
Postorder:
G H D BI E J K F C A
A
C
B
E
F
D
I
J
K
G
H
Operations:
Insert:
87
87
27
67
54
54
54, 17, 87, 11, 19, 18, 93
54
54
54
54
87
17
17
87
17
11
54
54
17
87
17
87
11
93
19
11
19
18
Delete:
Similar to the insert function, after deletion of a node, the BST property must be maintained.
There are 3 possible cases:
We just delete the node.
The node can be deleted by replacing the node by that child.
Node to be deleted has two children
Node to be deleted has two children
Steps - A:
OR
Steps - B:
Search-
for element X in Tree T (r is the value stored at root)
if T is empty
"X is not in the tree";
else {
if X = r
"X is found"
else {
if X < r
search the left tree
else
search the right tree
}
}
Using the data structure described in page 7, the above algorithm can be implemented in C as follows:
tree Binarysearch (ElementType X, tree T)
{if (T = NULL)
ERROR("X is not in the tree") ;
else {
if (X = T->element)
return T;
else {
if (X < T->element)
return Binarysearch (X, T->left);
else
return Binarysearch (X, T->right);
}
}
}
An AVL (Adelson-Velskii and Landis) tree is a balanced BST with the height O(log n)