Binary tree
The depth of a root is
0
ne<=
2^h which equivalent to h>=log2(ne)
Child
A node directly connected to another node when moving away from the root.
Parent
A node directly connected to another node when moving towards the root
Ancestor
A node reachable by repeated proceeding from child to parent
Descendant
A node reachable by repeated proceeding from parent to child
Internal node
A node with at least one child
Leaf
A node with no children
Binary tree
A tree in which each node can have at most 2 children
Use a dynamic link or pointer to represent
Binary tree
Create a node using a class
Class BinaryNode{ private: int element; BinaryNode *left, *right; Public: BinaryNode( int el=0, BinaryNode *lt=Null, Binarynode*rt=Null): element(el), left(lt), right(rt){} }
h
Height of a tree
To find the height of a tree
Ht=1+max(HL,Hr) where Hl is the height of the left tree and the HR is the height of the right tree
Depth of x =
Length of path from root to z
A binary try can give order in searching a element
Log n
ne
Number of external nodes (leaves)
Tree properties 1
Recursive data structure
Application of tree it can
Storing naturally hierarchical data
Tree property
The number of edges is equal to the number of nodes minus one edge= node -1
Root
The top node in a tree
To build a binary tree the first thing is to build
a node that is done in binary node class
node that have no children
are call leaves or external nodes. All other nodes are called internal nodes
The number of nodes at level k is
at most 2^k
every node (except the root) is
connected by an edge from exactly one other node
Edge or link
connection between one node to another
Root is the
distinguished node it is the ancestor of all nodes in tree
Tree can organize data
for quick search, insertion, deletion
There is a unique path
from the root to each node
Height of tree
height of root node
Tree are use to represent a
hierarchical relation between data
A binary tree is a tree
in which each node has exactly two subtrees: The left subtree and right subtree, either or both of which may be empty
A general rooted tree
is a set of nodes that has designated node called the root, from which zero or more subtrees descend
The height of a tree
is equal to the height of the root
Depth of a node in a tree
is the length of the path from the root to the node
Size of a node or degree of a node
is the number of descendants
Size of a tree
is the total number of nodes of this tree
the recursive definition of the binary tree
it is either empty or consists of a root, a left tree, and right tree.
each subtree
itself satisfies the definition of a tree
Height of a node is the
length of the longest path from a given node to the deepest left.
Each arrow is
link
h >=
log2(n+1)-1 since ne=(n+1)/2
A tree with n nodes must have
n-1 edges
N node =
n-1 edges
ne=
ni+1
h <=
ni<=(n-1)/2 since ni=(n-1)/2
an empty tree has
no nodes
Siblings
node with the same parent
Height of x
number of edges in longest path from x to a leaf
Depth
number of edges in path from root to x
ni
number of internal nodes
n
number of nodes
A tree is
set of node and directed edges(or Branches) connecting pairs of node
To find the size of a tree
st=1+Sl+Sr where sl is the sixe of the left tree and sr is the size of the right tree
When a node has no children
the corresponding member are null
sibling are node with
the same parent
each node has
three data
the middle of the node is
to hold data
the left of the node is
to store the address of the left child
the right of the cell is
to store the address of the right child
A node representation has
two Binarynode member, one for the left child and one for the right child and data object type
If there is a path from the node u to node v then
u is ancestor of v and v is a descendant of u