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. 



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值