binary trees in computer science, a binary tree is a tree data structure in which each node has at...

8
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; }

Upload: jasmine-newman

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

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; }

Page 2: 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

Tree Traversal

• Inorder Traversal• Preorder Traversal• Postorder Traversal• Level order Traversal

Page 3: 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

Postorder TraversalAlgorithm

1. Traverse the left subtree2. Traverse the right subtree3. 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); }

Page 4: 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

Inorder Traversal

Algorithm1. Traverse the left subtree2. 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); }

Page 5: 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

Preorder Traversal

Algorithm1. Visit the root.2. Traverse the left subtree3. Traverse the right subtree

void printPostorder (struct node* node) { if (node == NULL) return; printf("%d ", node->data); printTree(node->left); printTree(node->right); }

Page 6: 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

Level order Traversal

Level Order:

1, 2, 3, 4, 5

Use Queue

Page 7: 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

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.

Page 8: 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

Binary Search Tree