datatstructure 4
datatstructure 4
Definitions
Tree operations
Tree Traversals
TREES IN DATA STRUCTURES
A tree is a nonlinear hierarchical data
structure that consists of nodes connected by
edges.
A node is a structure which may contain a
node.
WHY TREE DATA STRUCTURE?
data
Trees are used to represent hierarchies
searching
Trees are very flexible data, allowing to move
compiler construction.
Trees are also used in database design.
Root
Parent − Any node except the root node has
one edge upward to a node called parent.
Child − The node below a given node
descendants of a node.
Degree of a Node:The degree of a node is
the total number of branches of that node.
Degree of a Tree : The degree of a tree is
B-Tree
Expression trees
Tournament trees
BINARY TREE
A binary tree is a tree data structure in which each
parent node can have at most two children.
Binary Tree
A tree is said to be binary tree when,
Binary Tree.
It is also known as a proper binary tree.
left.
The last leaf element might not have a right
Syntax tree
BINARY SEARCH TREES
A binary search tree is also a kind of binary
tree in which the nodes are arranged in an
order.
A BST is a binary tree where nodes are
Deletion
SEARCH OPERATION
The algorithm depends on the property of
BST that if each left subtree has values below
root and each right subtree has values above
the root.
If the value is below the root, we can say for
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
INSERT OPERATION
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
DELETION OPERATION
position.
6 to be deleted
Final tree
CASE III
original position.
3 to be deleted
in-order manner.
Postorder Traversal − Traverses a tree in a
post-order manner.
Every tree is a combination of
A node carrying data
Two subtrees
INORDER TRAVERSAL
Algorithm:
inorder(root->left)
display(root->data)
inorder(root->right)
INORDER:5 -> 12 -> 6 -> 1 -> 9
PREORDER TRAVERSAL
Algorithm:
display(root->data)
preorder(root->left)
preorder(root->right)
PREORDER:1->12->5->6->9
POSTORDER TRAVERSAL
Algorithm:
postorder(root->left)
postorder(root->right)
display(root->data)
POSTORDER:5->6->12->9->1
TREE TRAVERSAL USING C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
// PREORDER TRAVERSAL
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";
}
// INORDER TRAVERSAL
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
int main() {
struct Node* root = new Node(1);
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6);
B C D
G
E
H I
F
forest tree
ALGORITHM
1.Convert each tree to a binary tree
2.The first binary tree doesn’t move
Starting from the second binary tree, the root node
of the later binary tree is used as the right child of
the root node of the previous binary tree, and
connected by lines
B E
C F G
H
D
I
Binary Tree