Starting%20at%204.2%20-%20Binary%20Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Starting%20at%204.2%20-%20Binary%20Trees

Description:

Starting at 4.2 - Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 23
Provided by: WillT156
Category:

less

Transcript and Presenter's Notes

Title: Starting%20at%204.2%20-%20Binary%20Trees


1
Starting at 4.2 - Binary Trees
  • A binary tree is made up of a finite set of nodes
    that is either empty or consists of a node called
    the root together with two binary trees called
    the left and right subtrees which are disjoint
    from each other and from the root
  • Notation Node, Children, Edge, Parent, Ancestor,
    Descendant, Path, Depth, Height, Level, Leaf
    Node, Internal Node, Subtree.

A
C
B
D
F
E
G
I
H
2
Traversals
  • Any process for visiting the nodes in some order
    is called a traversal.
  • Any traversal that lists every node in the tree
    exactly once is called an enumeration of the
    trees nodes.
  • Preorder traversal Visit each node before
    visiting its children.
  • Postorder traversal Visit each node after
    visiting its children.
  • Inorder traversal Visit the left subtree, then
    the node, then the right subtree.
  • void preorder(BinNode root)
  • if (rootNULL) return
  • visit(root)
  • preorder(root-gtleftchild())
  • preorder(root-gtrightchild())

3
Expression Trees
  • Example of (a-b)/((xy3)-(6z))

/
-
-
a
b


6
z
3

y
x
4
Constructing an Expression Tree
  • Much easier with a postfix expression.
  • Study pages 122-123 to see how to do this.
  • Now as we scan from left to right the postfix
    expression
  • If the item is an operand, create a one node tree
    and push the pointer onto a stack
  • Else if the item is an operator, pop the 2
    operand pointers off the top of the stack, create
    a new node with left and right pointers to the 2
    popped nodes and push the new node onto the stack

5
Binary Search Trees
  • Left means less right means greater.
  • Find
  • If itemltcur-gtdat then curcur-gtleft
  • Else if itemgtcur-gtdat then curcur-gtright
  • Else found
  • Repeat while not found and cur not NULL
  • No need for recursion.
  • INSERT find until cur is NULL then insert after
    previous node.

6
Find min and max
  • The min will be all the way to the left
  • While cur-gtleft ! NULL, curcur-gtleft
  • The max will be all the way to the right
  • While cur-gtright !NULL, curcur-gtright
  • Insert
  • Like a find, but stop when you would go to a null
    and insert there.

7
Remove
  • If node to be deleted is a leaf (no children),
    can remove it and adjust the parent node (must
    keep track of previous)
  • If the node to be deleted has one child, remove
    it and have the parent point to that child.
  • If the node to be deleted has 2 children
  • Replace the data value with the smallest value in
    the right subtree
  • Delete the smallest value in the right subtree
    (it will have no left child, so a trivial delete.

8
Algorithm Analysis
  • If the tree is perfectly balanced, then the
    height of every leaf node is O(log2n).
  • Issues with best/worst/average case
  • Balance of the tree (usually based on building
    order)
  • Which data in the tree is being accessed.
  • Best tree shape perfectly balanced
  • Worst tree shape looks like a Linked List
  • Average case look at all the shapes of every
    possible tree for every size n O(log2n).

9
AVL Tree
  • A binary search tree with a balance condition.
  • Ensures the tree has height O(log n).
  • For every node, its left and right subtree
    heights can differ at most by 1.
  • The tree can become imbalanced after an insertion
  • The only nodes involved in fixing the imbalance
    will be in the path of the inserted node.

10
Imbalancing
  • A tree may become imbalanced at node n when
  • An insertion into the left subtree of the left
    child of n
  • An insertion into the right subtree of the left
    child of n
  • An insertion into the left subtree of the right
    child of n
  • An insertion into the right subtree of the right
    child of n

11
Single Rotation
  • Rotate right through n to fix case 1
  • Rotate left through n to fix case 4

k2
k1
k1
k2
12
Double Rotations
  • A single rotation does not fix case 2

k2
k1
k1
k2
13
Double rotations
  • Must look at more stuff
  • Similar for inserting left of right subtree

k3
k1
k1
k2
k2
k3
14
General Trees
  • A tree T is either empty or has a root node.
    This root node has pointers to some number of
    subtrees.
  • Each node can have a different number of pointers
    to its child trees.

15
General Tree Example
Root
R
Ancestors of V
P
V
S1
S2
C1
C2
Siblings of V
Children of V
Subtree rooted at V
16
Implementations of General Trees
  • Left Child/Right Sibling - Each node has a
    pointer to his leftmost child and a pointer to
    his right sibling (can also have a parent
    pointer).

17
2-3 Tree Insertion
  • Start with
  • Add 14, 55

18
Tree Splitting
  • When an addition overfills a node (gives it 3
    values), then the node needs to split.
  • Create 2 nodes - one with the left value, one
    with the right value and send the middle value to
    the parent node to insert there.
  • This insertion in the parent may cause it to
    split. The process is then repeated.
  • Can minimize splitting by shifting values among
    sibling nodes.

19
B-Trees
  • The B-tree is an extension of the 2-3 tree
  • The B-tree is THE standard file organization for
    applications requiring insertion, deletion and
    key range searches.
  • B-trees are always balanced.
  • B-trees keep related records on a disk page,
    which takes advantage of locality of reference.
  • B-trees guarantee that every node in the tree
    will be full at least to a certain minimum
    percentage. This improves space efficiency while
    reducing the typical number of disk fetches
    necessary during a search or update operation.

20
B-Tree Properties
  • A B-tree of order m has the following properties.
  • The root is either a leaf or has at least 2
    children.
  • Each node, except for the root and leaves, has
    between ?m/2? and m children.
  • All leaves are at the same level in the tree, so
    the tree is always height balanced.
  • A B-tree node is usually selected to match the
    size of a disk block.
  • A B-tree node could have hundreds of children.

21
B-Tree Search
  • Search in a B-tree is a generalization of search
    in a 2-3 tree.
  • Perform a binary search on the keys in the
    current node. If the search key is found, then
    return the record. If the current node is a leaf
    node and the key is not found, then report an
    unsuccessful search.
  • Otherwise, follow the proper branch and repeat
    the process.

22
B Trees
  • Internal nodes of the B tree do not store
    records, only key values to guide the search.
  • Leaf nodes store records or pointers to to the
    records.
  • A leaf node has a pointer to the next sibling
    node. This allows for sequential processing.
  • An internal node with 3 keys has 4 pointers. The
    3 keys are the smallest values in the last 3
    nodes pointed to by the 4 pointers. The first
    pointer points to nodes with values less than the
    first key.
Write a Comment
User Comments (0)
About PowerShow.com