Skip to content

Commit 6ac44fb

Browse files
committed
full binary tree
1 parent 423a3a4 commit 6ac44fb

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Python implementation of Binary Tree
2+
3+
class Node:
4+
def __init__(self, item):
5+
self.left = None
6+
self.right = None
7+
self.val = item
8+
9+
def inOrder(self):
10+
if self.left:
11+
self.left.inOrder()
12+
print(self.val, end=' ')
13+
if self.right:
14+
self.right.inOrder()
15+
16+
def preOrder(self):
17+
print(self.val, end=' ')
18+
if self.left:
19+
self.left.preOrder()
20+
if self.right:
21+
self.right.preOrder()
22+
23+
def postOrder(self):
24+
if self.left:
25+
self.left.postOrder()
26+
if self.right:
27+
self.right.postOrder()
28+
print(self.val, end=' ')
29+
30+
# Driver Code
31+
root = Node(1)
32+
root.left = Node(2)
33+
root.right = Node(3)
34+
root.left.left = Node(4)
35+
36+
print("Pre order Traversal: ", end="")
37+
root.preOrder()
38+
print("\nIn order Traversal: ", end="")
39+
root.inOrder()
40+
print("\nPost order Traversal: ", end="")
41+
root.postOrder()
42+
43+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Python implementation of checking for full binary tree
2+
3+
class Node:
4+
def __init__(self, item):
5+
self.leftC = None
6+
self.rightC = None
7+
self.item = item
8+
9+
def isFullBinaryTree(root):
10+
11+
# Empty Tree
12+
if root is None:
13+
return True
14+
15+
if root.leftC is None and root.rightC is None:
16+
return True
17+
18+
if root.leftC is not None and root.rightC is not None:
19+
return isFullBinaryTree(root.leftC) and isFullBinaryTree(root.rightC)
20+
21+
return False
22+
23+
# Driver Code
24+
root = Node(1)
25+
root.rightChild = Node(3)
26+
root.leftChild = Node(2)
27+
root.leftChild.leftChild = Node(4)
28+
root.leftChild.rightChild = Node(5)
29+
root.leftChild.rightChild.leftChild = Node(6)
30+
root.leftChild.rightChild.rightChild = Node(7)
31+
32+
if(isFullBinaryTree(root)):
33+
print("The tree is a full binary tree")
34+
else:
35+
print("The tree is not a full binary tree")

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,24 @@
218218

219219
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/post-order-traversal.gif)
220220

221-
### [Binary Tree](/Practice%20Concepts/Trees/tree-traverse.py)
221+
### [Binary Tree](/Practice%20Concepts/Trees/binary-tree.py)
222222
* Tree data structure in which each parent node can have at most 2 children. Each node contains - data item, address of left child, address of right child.
223-
- Full Binary Tree:
223+
- [Full Binary Tree](/Practice%20Concepts/Trees/full-binary-tree.py):
224224
- Every parent/internal node has either 2 or no children nodes.
225225

226226
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/full-binary-tree.png)
227227

228228
- Also known as a proper binary tree
229-
- Let i = number of internal nodes
230-
n = total number of nodes
231-
l = number of leaves
232-
λ = number of levels
233-
-
229+
- Let i = number of internal nodes |
230+
n = total number of nodes |
231+
l = number of leaves |
232+
λ = number of levels
233+
- Number of leaves = i + 1
234+
- n = 2i + 1
235+
- l = (n + 1) / 2
236+
- i = l – 1
237+
- l is at most 2<sup>λ</sup> - 1
238+
234239

235240

236241

0 commit comments

Comments
 (0)