Linear Tree or Complete binary tree using Tree Data Structure

Linear Tree or Complete binary tree using Tree Data Structure

 

 

Complete Binary Tree: 

        A complete binary tree is a tree in which every level, except possibly the last, is completely filled, and all nodes are as left as possible. This structure is particularly interesting because it can be efficiently represented using an array.

Here's an example of a complete binary tree:

 

Code:

```py

class Node:
def __init__(self, value):
self.data = value
self.left = None
self.right = None

def Insert(root, val):
if root is None:
return Node(val)
queue = [root]
while queue:
current = queue.pop(0)
if current.left is None:
current.left = Node(val)
return
elif current.right is None:
current.right = Node(val)
return
else:
queue.append(current.left)
queue.append(current.right)

def Inorder(root):
if root:
Inorder(root.left)
print(root.data, end=" ")
Inorder(root.right)

root = Node(1)
Insert(root, 2)
Insert(root, 3)
Insert(root, 4)
Insert(root, 5)
Insert(root, 6)
Insert(root, 7)
print(end="Inorder: ")
Inorder(root)
print()

``` 

Sample input/Output:

Tree Insertion:

  

 

....Thank You....

Comments

Popular posts from this blog

Convert binary to decimal in c using built in function

Install nodeJs in linux

DFS in Complete Tree