data); }"> data); }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.

Similar presentations


Presentation on theme: "Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left."— Presentation transcript:

1 Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. struct node { int data; struct node* left; struct node* right; }

2 Tree Traversal Inorder Traversal Preorder Traversal Postorder Traversal Level order Traversal

3 Postorder Traversal Algorithm 1.Traverse the left subtree 2.Traverse the right subtree 3. Visit the root. void printPostorder (struct node* node) { if (node == NULL) return; // first recur on both subtrees printTree(node->left); printTree(node->right); // then deal with the node printf("%d ", node->data); }

4 Inorder Traversal Algorithm 1.Traverse the left subtree 2. Visit the root. 3. Traverse the right subtree void printPostorder (struct node* node) { if (node == NULL) return; printTree(node->left); printf("%d ", node->data); printTree(node->right); }

5 Preorder Traversal Algorithm 1. Visit the root. 2.Traverse the left subtree 3.Traverse the right subtree void printPostorder (struct node* node) { if (node == NULL) return; printf("%d ", node->data); printTree(node->left); printTree(node->right); }

6 Level order Traversal Level Order: 1, 2, 3, 4, 5 Use Queue

7 Binary Search Tree The common properties of binary search trees are as follows: 1. The left subtree of a node contains only nodes with keys less than the node's key. 2. The right subtree of a node contains only nodes with keys greater than the node's key. 3. The left and right subtree each must also be a binary search tree. 4. Each node can have up to two successor nodes. 5. There must be no duplicate nodes. 6. A unique path exists from the root to every other node.

8 Binary Search Tree


Download ppt "Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left."

Similar presentations


Ads by Google