Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cae7f8b

Browse files
three
1 parent a6205b1 commit cae7f8b

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎Tree Theory/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Binary Tree
2+
3+
- Node's are called structures with a maximum of 2 children.
4+
- Since the number of children will be maximum 2, their names are identified as left and right.
5+
- Like left child and right child.
6+
- The maximum number of nodes at a given level is 2 ^ (l-1), l = level number.
7+
- For example, the maximum number of nodes in the third level 2 ^ 3 = 8.
8+
- height = the total number of paths from the desired node to the leaf below.
9+
- root height = 3 = height tree.
10+
- leaves height = 0
11+
12+
## Some important tree types
13+
14+
- **Full Binary Tree**: Each node has 0 or 2 children.
15+
- **Complete Binary**: Either all their levels will be full, or at least their nodes will be on the left as much as possible.
16+
- **Perfect Binary Tree**: All nodes will have 2 children plus all leaves will be in the same level.
17+
- **A degenerate (or pathological) tree**: If each node has 1 child. As you may have noticed, it looks very similar to the linked list.
18+
19+
<img src="https://miro.medium.com/max/16000/1*CMGFtehu01ZEBgzHG71sMg.png" width="500" />
20+
21+
# Binary Seach Tree
22+
- It requires that all values ​​that can be reached on each node from its left arm are smaller than the value of the node and that all values ​​that can be reached from the right arm must be greater than the value of that node.
23+
And of course it also has to be a binary tree.
24+
- Because of ordering in BST, search is done fast.
25+
- In the worst case, the time complexity of searching and inserting operations is O(h). (h=height).

‎Tree Theory/binary_search_tree.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Node:
2+
def __init__(self,key):
3+
"""
4+
constructor
5+
"""
6+
self.val = key
7+
self.left = None
8+
self.right = None
9+
10+
11+
def insert(root,node):
12+
if root in None:
13+
root = node
14+
else:
15+
if root.val < node.val:
16+
if root.right is None:
17+
root.right = node
18+
else:
19+
insert(root.right, node)
20+
else:
21+
if root.left is None:
22+
root.left = node
23+
else:
24+
insert(root.left, node)
25+
26+
"""
27+
8
28+
/ \
29+
5 11
30+
/ \ / \
31+
2 7 9 18
32+
"""
33+
r = Node(8)
34+
insert(r, Node(5))
35+
insert(r, Node(2))
36+
insert(r, Node(7))
37+
insert(r, Node(9))
38+
insert(r, Node(18))
39+
insert(r, Node(11))
40+
41+
def inorder(root):
42+
if root:
43+
inorder(root.left)
44+
print(root.val)
45+
inorder(root.right)
46+
else:
47+
return None
48+
49+
inorder(r)

‎Tree Theory/binary_tree.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Node:
2+
"""
3+
binary tree nodes
4+
"""
5+
def __init__(self,key):
6+
"""
7+
constructor
8+
"""
9+
self.val = key
10+
self.right = None
11+
self.left = None
12+
13+
14+
15+
"""
16+
Create Node
17+
18+
a
19+
/ \
20+
b c
21+
/
22+
d
23+
"""
24+
root = Node('A')
25+
root.left = Node('B')
26+
root.left.left = Node('D')
27+
root.right = Node('C')

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /