0% found this document useful (0 votes)
0 views3 pages

Tree

The document discusses binary trees, including classifications based on the number of children and level completion. It outlines various types of binary trees such as full, degenerate, complete, perfect, and balanced trees. Additionally, it provides implementations for tree traversal methods: in-order, pre-order, post-order, and breadth-first search (BFS).

Uploaded by

hikalandsomeone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Tree

The document discusses binary trees, including classifications based on the number of children and level completion. It outlines various types of binary trees such as full, degenerate, complete, perfect, and balanced trees. Additionally, it provides implementations for tree traversal methods: in-order, pre-order, post-order, and breadth-first search (BFS).

Uploaded by

hikalandsomeone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//Binary tree

Max num of children of level if i start from level 0= 2^level


Max num of children of whole tree if i start from level 1= 2^h+1 -1

//Classification according to number of children


1-Full binary tree: each node have 0 or 2 children
2-Degenerate binary tree: each node have 0 or 1 child
3-Skewed binary tree: same as degenerate but all in 1 direction either left or
right

//Classification according to completeion of level


1-Complete binary tree: all levels are full except the last level can have
missing node(on the right side only)
2-Perfect binary tree: all parent have 2 childs and all on same level
3-Balanced binary tree: different in height between left and right do not exceed
1 (deapth=hLeft-hRight)<=1

//In Order traversal(Left,Root,Right)


template <class T>
void inOrder(BinaryTreeNode<T> *t){
if(t){
inOrder(t->LeftChild);
visit(t);
inOrder(t->RightChild);
}
}
A
B C
E F G
H
I
//EBFACGIH

//Pre Order traversal(Root,Left,Right)


template <class T>
void preOrder(BinaryTreeNode<T> *t){
if(t){
visit(t);
preOrder(t->LeftChild);
preOrder(t->RightChild);
}
}
A
B C
E F G
H
I
//ABEFCGHI

//Post Order traversal(Left,Right,Root)


template<class T>
void PostOrder(BinaryTreeNode<T> *t){
if(t){
PostOrder(t->LeftChild);
PostOrder(t->RightChild);
visit(t);
}
}
A
B C
E F G
H
I
//EFBIHGCA

//BFS
template<class T>
void LevelOrder(BinaryTreeNode<T> *t){
LinkedQueue<BinaryTreeNode<T>*> Q;
while(t){
visit(t);
if(t->LeftChild) Q.Add(t->LeftChild);
if(t->RightChild) Q.Add(t->RightChild);
try{Q.Delete(t);}
catch{OutOfBounds} {return;}
}
}
//Test your code in main
BinaryTreeNode<int> x,y,z;
x.data=1;
y.data=2;
z.data=3;

x.LeftChild=&y;
x.RightCild=&z;

InOrder(&x);
cout<<endl;
//pre,post,level Same

You might also like