Binary Tree

advertisement
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Data Structure using C++
Lecture 03
TREES
Definition of Tree:
 A tree t is a finite nonempty set of elements
 One of these elements is called the root
 The remaining elements, if any, are partitioned into trees, which are
called the subtrees of t.
 Tree
 Nodes
 Each node can have 0 or more children
 A node can have at most one parent
 Binary tree
 Tree with 0–2 children per node
 Terminology
 Root  no parent
 Leaf  no child
 Interior  non-leaf
 Height  distance from root to leaf
1
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Tree Terminology:
 The element at the top of the hierarchy is the root.
 Elements next in the hierarchy are the children of the root.
 Elements next in the hierarchy are the grandchildren of the root, and so on.
 Elements at the lowest level of the hierarchy are the leaves.
Other Definitions:
 Leaves, Parent, Grandparent, Siblings, Ancestors, Descendents
Leaves = {Mike,AI,Sue,Chris}
Parent(Mary) = Joe
Grandparent(Sue) = Mary
Siblings(Mary) = {Ann,John}
Ancestors(Mike) = {Ann,Joe}
Descendents(Mary)={Mark,Sue}
Levels and Height:
 Root is at level 1 and its children are at level 2.
 Height = depth = number of levels
2
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Node Degree:
 Node degree is the number of children it has
Tree Degree:
 Tree degree is the maximum of node degrees
tree degree = 3
3
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Binary Tree:
 A finite (possibly empty) collection of elements
 A nonempty binary tree has a root element and the remaining elements (if
any) are partitioned into two binary trees
 They are called the left and right subtrees of the binary tree
Difference Between a Tree & a Binary Tree:
 A binary tree may be empty; a tree cannot be empty.
 No node in a binary tree may have a degree more than 2, whereas there is
no limit on the degree of a node in a tree.
 The subtrees of a binary tree are ordered; those of a tree are not ordered.
Binary Search Trees:
 Key property
 Value at node
 Smaller values in left subtree
 Larger values in right subtree
 Example
 X>Y
 X<Z
4
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Example Binary Searches:
 Find ( root, 2 )
 Find (root, 25 )
Types of Binary Trees:
 Degenerate – only one child
 Complete – always two children
 Balanced – “mostly” two children
 more formal definitions exist, above are intuitive ideas
5
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Binary Search Tree Construction:
 How to build & maintain binary trees?
 Insertion
 Deletion
 Maintain key property (invariant)
 Smaller values in left subtree
 Larger values in right subtree
Binary Search Tree – Insertion:
 Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for Y
 Observations
1. O( log(n) ) operation for balanced tree
2. Insertions may unbalance tree
Example Insertion:
 Insert ( 20 )
6
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Binary Search Tree – Deletion:
 Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
 O( log(n) ) operation for balanced tree
 Deletions may unbalance tree
Example Deletion (Leaf):
 Delete ( 25 )
Example Deletion (Internal Node):
 Delete ( 10 )
7
‫م اثير هادي الرماحي‬.‫م‬
802‫ح‬+832‫ح‬
DATA STRUCTURES 2
Binary Tree for Expressions:
8
‫م اثير هادي الرماحي‬.‫م‬
Download