CC chapter 4 Tree and Graphs ----Tree


1. Type of Trees

A tree is a data structure composed of nodes.

  •           Each tree has a root node.
  •           The root node has zero or more child nodes.
  •           Each child node has zero or more child nodes, and So on. 

      Definition:

          class Node{
                  public String name;
                  public Node[] children;
          }


 Trees vs. Binary Trees

        A binary tree is a tree in which each node has up to two children.  

Binary Tree vs. Binary Search Tree

       A binary search tree is a binary tree in which every node fits a specific ordering property: 

       all left descendents  <= n < all right descendents. 


Balanced vs.Unbalanced

       It is balanced enough to ensure O(logn) times for insert and find but it's not necessarily as balanced as it could be. 


Complete Binary Trees

A complete binary tree is a binary tree in which every level of the tree is fully filled, except for perhaps the last level. To the extent that the last level is filled, it is filled left to right. 


Full Binary trees

A full binary tree is a binary tree in which every node has either zero or two children. That is, no nodes have only one child.


Perfect Binary Trees 

A perfect binary tree is one that is both full and complete. All leaf nodes will be at the same level, and this level has the maximum number of nodes. 


2. Binary Tree Traversal

In-Order Traversal

void inOrderTraversal(TreeNode node)
{
        if(node != null)
        {
                inOrderTraversal(node.left);
                visit(node);
                inOrderTraversal(node.right);
         }
}

Pre-Order Traversal

void preOrderTraversal(TreeNode node){
       if(node != null){
        visit(node);
         preOrderTraversal(node.left);
         preOrderTraversal(node.right);
    }    
}


Post-Order Traversal

void postOrderTraversal(TreeNode node)
{
          if(node != null)
          {
                 postOrderTraversal(node.left);
                 postOrderTraversal(node.right);
                 visit(node);
          }
}

3. Binary Heaps (Min-Heaps and Max-Heaps)

A min-heap is a complete binary tree(that is, totally filled other than the rightmost elements on the last level) where each node is smaller than its children. The root is the minimum element in the tree. 


Operations: insert()   and extract_min()

  insert(): (1) insert into bottom rightmost spot. (2) swap the new element with its parent, until we find an appropriate spot .  Time complexity: O(log n)

  Extract Minimum Element.   Time complexity: O(log n)

(1) Remove the minimum element and swap it with the last element in the heap( the bottommost, rightmost element). 

(2) Buddle down this element, swapping it with one of its children until the min-heap property is restored. 


4. Tries (Prefix Trees)

A trie is a variant of an n-ary tree in which characters are stored at each node. Each path down the tree may represent a word. 



你可能感兴趣的:(数据结构,Data,Structure)