Lecture04 Tree EN
Lecture04 Tree EN
CONTENTS
• a tree as a set of nodes that either is empty or has a node called the
root that is connected by edges to zero or more subtrees to form a
hierarchical structure. Each subtree is itself by definition a tree.
• Every node, except the root, has a parent node, which is identified by
the incoming edge.
• The children of a node are identified by the outgoing edges.
• Nodes that have at least one child are known as interior nodes while
nodes that have no children are known as leaf nodes.
• A binary tree is a tree in which each node can have at most two
children. One child is identified as the left child and the other as the
right child.
• The nodes in a binary tree are organized into levels with the root node
at level 0, its children at level 1, the children of level one nodes are at
level 2, and so on.
• The depth of a node is its distance from the root. A node's depth
corresponds to the level it occupies.
• The height of a binary tree is is the length of the path from the root to
the deepest node in the tree. It is the maximum level among all the
nodes in the tree.
• The width of a binary tree is the number of nodes on the level
containing the most nodes.
• A full binary tree is a binary tree in which each interior node contains
two children.
• A perfect binary tree is a full binary tree in which all leaf nodes are at
the same level.
• Idea:
• If the tree is None, nothing to do
• Else
• Visit the root node.
• Traverse the left subtree
• Traverse the right subtree.
• Function:
Result: A, B, D, E, H, C, F, G, I, J.
• Idea:
• If the tree is None, nothing to do
• Else
• Traverse the left subtree
• Visit the root node.
• Traverse the right subtree.
• Function:
Result: D, B, H, E, A, F, C, I, G, J.
• Idea:
• If the tree is None, nothing to do
• Else
• Traverse the left subtree
• Traverse the right subtree.
• Visit the root node.
• Function:
Result: D, H, E, B, F, I, J, G, C, A.
• Idea
• Idea:
• Idea