From 10e40af9e97a57945f5a96ca1c2890ce17844cdb Mon Sep 17 00:00:00 2001 From: Ishita-Jena <167574361+Ishita-Jena@users.noreply.github.com> Date: Sun, 9 Jun 2024 04:01:25 +0530 Subject: [PATCH 1/7] Create binary_tree.md --- contrib/ds-algorithms/binary_tree.md | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 contrib/ds-algorithms/binary_tree.md diff --git a/contrib/ds-algorithms/binary_tree.md b/contrib/ds-algorithms/binary_tree.md new file mode 100644 index 0000000..fb11230 --- /dev/null +++ b/contrib/ds-algorithms/binary_tree.md @@ -0,0 +1,71 @@ +# Binary Tree + +A binary tree is a non-linear data structure in which each node can have atmost two children, known as the left and the right child. + +```python + A...................Level 0 + / \ + B C.................Level 1 + / \ \ + D E G...............Level 2 +``` + +## Basic Terminologies + +- **Root node:** The topmost node in a tree is the root node. The root node does not have any parent. In the example given above, **A** is the root node. +- **Parent node:** The predecessor of a node is called the parent of that node. **A** is the parent of **B** and **C**, **B** is the parent of **D** and **E** and **C** is the parent of **G**. +- **Child node:** The successor of a node is called the child of that node. **B** and **C** are children of **A**, **D** and **E** are children of **B** and **G** is the right child of **C**. +- **Leaf node:** Nodes without any children are called the leaf nodes. **D**, **E** and **G** are the leaf nodes. +- **Ancestor node:** Predecessor nodes on the path from the root to that node are called ancestor nodes. **A** and **B** are the ancestors of **E**. +- **Descendant node:** Successor nodes on the path from the root to that node are called descendant nodes. **B** and **E** are descendants of **A**. +- **Sibling node:** Nodes having the same parent are called sibling nodes. **B** and **C** are sibling nodes and so are **D** and **E**. +- **Level (Depth) of a node:** Number of edges in the path from the root to that node is the level of that node. The root node is always at level 0. The depth of root node is the depth of the tree. Nodes **D**, **E** and **G** are at level 2. +- **Height of a node:** Number of edges in the path from that node to the deepest leaf. The height of the root is the height of a tree. Height of nodes **B** and **C** are 1. + +## Types Of Binary Trees + +- **Full Binary Tree:** A binary tree where each node has 0 or 2 children is a full binary tree. +```python + A + / \ + B C + / \ + D E +``` +- **Complete Binary Tree:** A binary tree in which all levels are completely filled except the last level is a complete binary tree. Whenever new nodes are inserted, they are inserted from the left side. +```python + A + / \ + / \ + B C + / \ / + D E F +``` +- **Perfect Binary Tree:** A binary tree in which all nodes are completely filled, i.e., each node has two children is called a perfect binary tree. +```python + A + / \ + / \ + B C + / \ / \ + D E F G +``` +- **Skewed Binary Tree:** A binary tree in which each node has either 0 or 1 child is called a skewed binary tree. It is of two types - left skewed binary tree and right skewed binary tree. +```python + A A + \ / + B B + \ / + C C + Right skewed binary tree Left skewed binary tree +``` +- **Balanced Binary Tree:** A binary tree in which the height difference between the left and right subtree is not more than one and the subtrees are also balanced is a balanced binary tree. +```python + A + / \ + B C + / \ + D E +``` + +## Implementation of Binary Tree From ba59ef608ca96405abc6f9a9c14d2b1a26ed5ffe Mon Sep 17 00:00:00 2001 From: Ishita-Jena <167574361+Ishita-Jena@users.noreply.github.com> Date: Sun, 9 Jun 2024 04:02:49 +0530 Subject: [PATCH 2/7] Update index.md --- contrib/ds-algorithms/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contrib/ds-algorithms/index.md b/contrib/ds-algorithms/index.md index 9c05b15..ecc4a10 100644 --- a/contrib/ds-algorithms/index.md +++ b/contrib/ds-algorithms/index.md @@ -15,4 +15,5 @@ - [Trie](trie.md) - [Two Pointer Technique](two-pointer-technique.md) - [Hashing through Linear Probing](hashing-linear-probing.md) -- [Hashing through Chaining](hashing-chaining.md) \ No newline at end of file +- [Hashing through Chaining](hashing-chaining.md) +- [Binary Tree](binary_tree.md) From 4e33c752407656390fd2b89398c837c67959bfcb Mon Sep 17 00:00:00 2001 From: Ishita-Jena <167574361+Ishita-Jena@users.noreply.github.com> Date: Sun, 9 Jun 2024 12:08:02 +0530 Subject: [PATCH 3/7] Update binary_tree.md --- contrib/ds-algorithms/binary_tree.md | 168 ++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 4 deletions(-) diff --git a/contrib/ds-algorithms/binary_tree.md b/contrib/ds-algorithms/binary_tree.md index fb11230..4891204 100644 --- a/contrib/ds-algorithms/binary_tree.md +++ b/contrib/ds-algorithms/binary_tree.md @@ -1,6 +1,6 @@ # Binary Tree -A binary tree is a non-linear data structure in which each node can have atmost two children, known as the left and the right child. +A binary tree is a non-linear data structure in which each node can have atmost two children, known as the left and the right child. It is a heirarchial data structure represented in the following way: ```python A...................Level 0 @@ -12,15 +12,15 @@ A binary tree is a non-linear data structure in which each node can have atmost ## Basic Terminologies -- **Root node:** The topmost node in a tree is the root node. The root node does not have any parent. In the example given above, **A** is the root node. +- **Root node:** The topmost node in a tree is the root node. The root node does not have any parent. In the above example, **A** is the root node. - **Parent node:** The predecessor of a node is called the parent of that node. **A** is the parent of **B** and **C**, **B** is the parent of **D** and **E** and **C** is the parent of **G**. - **Child node:** The successor of a node is called the child of that node. **B** and **C** are children of **A**, **D** and **E** are children of **B** and **G** is the right child of **C**. - **Leaf node:** Nodes without any children are called the leaf nodes. **D**, **E** and **G** are the leaf nodes. - **Ancestor node:** Predecessor nodes on the path from the root to that node are called ancestor nodes. **A** and **B** are the ancestors of **E**. - **Descendant node:** Successor nodes on the path from the root to that node are called descendant nodes. **B** and **E** are descendants of **A**. - **Sibling node:** Nodes having the same parent are called sibling nodes. **B** and **C** are sibling nodes and so are **D** and **E**. -- **Level (Depth) of a node:** Number of edges in the path from the root to that node is the level of that node. The root node is always at level 0. The depth of root node is the depth of the tree. Nodes **D**, **E** and **G** are at level 2. -- **Height of a node:** Number of edges in the path from that node to the deepest leaf. The height of the root is the height of a tree. Height of nodes **B** and **C** are 1. +- **Level (Depth) of a node:** Number of edges in the path from the root to that node is the level of that node. The root node is always at level 0. The depth of root node is the depth of the tree. +- **Height of a node:** Number of edges in the path from that node to the deepest leaf is the height of that node. The height of the root is the height of a tree. Height of node **A** is 2, nodes **B** and **C** is 1 and nodes **D**, **E** and **G** is 0. ## Types Of Binary Trees @@ -68,4 +68,164 @@ A binary tree is a non-linear data structure in which each node can have atmost D E ``` +## Real Life Applications Of Binary Tree + +- **File Systems:** File systems employ binary trees to organize the folders and files, facilitating efficient search and access of files. +- **Decision Trees:** Decision tree, a supervised learning algorithm, utilizes binary trees, with each node representing a decision and its edges showing the possible outcomes. +- **Routing Algorithms:** In routing algorithms, binary trees are used to efficiently transfer data packets from the source to destination through a network of nodes. +- **Searching and sorting Algorithms:** Searching algorithms like binary search and sorting algorithms like heapsort rely heavily on binary trees. + ## Implementation of Binary Tree + +```python +from collections import deque + +class Node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + +class Binary_tree: + @staticmethod + def insert(root, data): + if root is None: + return Node(data) + q = deque() + q.append(root) + while q: + temp = q.popleft() + if temp.left is None: + temp.left = Node(data) + break + else: + q.append(temp.left) + if temp.right is None: + temp.right = Node(data) + break + else: + q.append(temp.right) + return root + + @staticmethod + def inorder(root): + if not root: + return + b.inorder(root.left) + print(root.data, end=" ") + b.inorder(root.right) + + @staticmethod + def preorder(root): + if not root: + return + print(root.data, end=" ") + b.preorder(root.left) + b.preorder(root.right) + + @staticmethod + def postorder(root): + if not root: + return + b.postorder(root.left) + b.postorder(root.right) + print(root.data, end=" ") + + @staticmethod + def levelorder(root): + if not root: + return + q = deque() + q.append(root) + while q: + temp = q.popleft() + print(temp.data, end=" ") + if temp.left is not None: + q.append(temp.left) + if temp.right is not None: + q.append(temp.right) + + @staticmethod + def delete(root, value): + q = deque() + q.append(root) + while q: + temp = q.popleft() + if temp is value: + temp = None + return + if temp.right: + if temp.right is value: + temp.right = None + return + else: + q.append(temp.right) + if temp.left: + if temp.left is value: + temp.left = None + return + else: + q.append(temp.left) + + @staticmethod + def delete_value(root, value): + if root is None: + return None + if root.left is None and root.right is None: + if root.data == value: + return None + else: + return root + x = None + q = deque() + q.append(root) + temp = None + while q: + temp = q.popleft() + if temp.data == value: + x = temp + if temp.left: + q.append(temp.left) + if temp.right: + q.append(temp.right) + if x: + y = temp.data + x.data = y + b.delete(root, temp) + return root + +b = Binary_tree() +root = None +root = b.insert(root, 10) +root = b.insert(root, 20) +root = b.insert(root, 30) +root = b.insert(root, 40) +root = b.insert(root, 50) +root = b.insert(root, 60) + +print("Preorder traversal:", end=" ") +b.preorder(root) + +print("\nInorder traversal:", end=" ") +b.inorder(root) + +print("\nPostorder traversal:", end=" ") +b.postorder(root) + +print("\nLevel order traversal:", end=" ") +b.levelorder(root) + +root = b.delete_value(root, 20) +print("\nLevel order traversal after deletion:", end=" ") +b.levelorder(root) + + +''' +OUTPUT: +Preorder traversal: 10 20 40 50 30 60 +Inorder traversal: 40 20 50 10 60 30 +Postorder traversal: 40 50 20 60 30 10 +Level order traversal: 10 20 30 40 50 60 +Level order traversal after deletion: 10 60 30 40 50 +''' +``` From eee9f149f251c50fffcaede4c02a3a02610b60cc Mon Sep 17 00:00:00 2001 From: Ishita-Jena <167574361+Ishita-Jena@users.noreply.github.com> Date: Sun, 9 Jun 2024 12:26:28 +0530 Subject: [PATCH 4/7] Update binary_tree.md --- contrib/ds-algorithms/binary_tree.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/ds-algorithms/binary_tree.md b/contrib/ds-algorithms/binary_tree.md index 4891204..867bca0 100644 --- a/contrib/ds-algorithms/binary_tree.md +++ b/contrib/ds-algorithms/binary_tree.md @@ -73,7 +73,7 @@ A binary tree is a non-linear data structure in which each node can have atmost - **File Systems:** File systems employ binary trees to organize the folders and files, facilitating efficient search and access of files. - **Decision Trees:** Decision tree, a supervised learning algorithm, utilizes binary trees, with each node representing a decision and its edges showing the possible outcomes. - **Routing Algorithms:** In routing algorithms, binary trees are used to efficiently transfer data packets from the source to destination through a network of nodes. -- **Searching and sorting Algorithms:** Searching algorithms like binary search and sorting algorithms like heapsort rely heavily on binary trees. +- **Searching and sorting Algorithms:** Searching algorithms like binary search and sorting algorithms like heapsort heavily rely on binary trees. ## Implementation of Binary Tree From 6822f4238aa09e266d340bab9d82950181721d4f Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 22 Jun 2024 15:44:24 +0530 Subject: [PATCH 5/7] Update binary_tree.md --- contrib/ds-algorithms/binary_tree.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/contrib/ds-algorithms/binary_tree.md b/contrib/ds-algorithms/binary_tree.md index 867bca0..03da2cf 100644 --- a/contrib/ds-algorithms/binary_tree.md +++ b/contrib/ds-algorithms/binary_tree.md @@ -2,7 +2,7 @@ A binary tree is a non-linear data structure in which each node can have atmost two children, known as the left and the right child. It is a heirarchial data structure represented in the following way: -```python +``` A...................Level 0 / \ B C.................Level 1 @@ -25,7 +25,7 @@ A binary tree is a non-linear data structure in which each node can have atmost ## Types Of Binary Trees - **Full Binary Tree:** A binary tree where each node has 0 or 2 children is a full binary tree. -```python +``` A / \ B C @@ -33,7 +33,7 @@ A binary tree is a non-linear data structure in which each node can have atmost D E ``` - **Complete Binary Tree:** A binary tree in which all levels are completely filled except the last level is a complete binary tree. Whenever new nodes are inserted, they are inserted from the left side. -```python +``` A / \ / \ @@ -42,7 +42,7 @@ A binary tree is a non-linear data structure in which each node can have atmost D E F ``` - **Perfect Binary Tree:** A binary tree in which all nodes are completely filled, i.e., each node has two children is called a perfect binary tree. -```python +``` A / \ / \ @@ -51,7 +51,7 @@ A binary tree is a non-linear data structure in which each node can have atmost D E F G ``` - **Skewed Binary Tree:** A binary tree in which each node has either 0 or 1 child is called a skewed binary tree. It is of two types - left skewed binary tree and right skewed binary tree. -```python +``` A A \ / B B @@ -60,7 +60,7 @@ A binary tree is a non-linear data structure in which each node can have atmost Right skewed binary tree Left skewed binary tree ``` - **Balanced Binary Tree:** A binary tree in which the height difference between the left and right subtree is not more than one and the subtrees are also balanced is a balanced binary tree. -```python +``` A / \ B C @@ -218,14 +218,14 @@ b.levelorder(root) root = b.delete_value(root, 20) print("\nLevel order traversal after deletion:", end=" ") b.levelorder(root) +``` +#### OUTPUT -''' -OUTPUT: +``` Preorder traversal: 10 20 40 50 30 60 Inorder traversal: 40 20 50 10 60 30 Postorder traversal: 40 50 20 60 30 10 Level order traversal: 10 20 30 40 50 60 Level order traversal after deletion: 10 60 30 40 50 -''' ``` From 00949d96ec517120f7eac1adc946bc1bd9aeaffa Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 22 Jun 2024 15:44:45 +0530 Subject: [PATCH 6/7] Rename binary_tree.md to binary-tree.md --- contrib/ds-algorithms/{binary_tree.md => binary-tree.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contrib/ds-algorithms/{binary_tree.md => binary-tree.md} (100%) diff --git a/contrib/ds-algorithms/binary_tree.md b/contrib/ds-algorithms/binary-tree.md similarity index 100% rename from contrib/ds-algorithms/binary_tree.md rename to contrib/ds-algorithms/binary-tree.md From 1c0d60b5d67b9155c473eb65549fd7850d2369dc Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 22 Jun 2024 15:45:35 +0530 Subject: [PATCH 7/7] Update index.md --- contrib/ds-algorithms/index.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contrib/ds-algorithms/index.md b/contrib/ds-algorithms/index.md index dda6713..f6249d1 100644 --- a/contrib/ds-algorithms/index.md +++ b/contrib/ds-algorithms/index.md @@ -16,8 +16,7 @@ - [Two Pointer Technique](two-pointer-technique.md) - [Hashing through Linear Probing](hashing-linear-probing.md) - [Hashing through Chaining](hashing-chaining.md) -- [Binary Tree](binary_tree.md) +- [Binary Tree](binary-tree.md) - [AVL Trees](avl-trees.md) - [Splay Trees](splay-trees.md) -- [Splay Trees](splay-trees.md) - [Dijkstra's Algorithm](dijkstra.md)