binary trees in computer science, a binary tree is a tree data structure in which each node has at...
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022083009/5697bfe11a28abf838cb3851/html5/thumbnails/8.jpg)
Binary Search Tree