zoukankan      html  css  js  c++  java
  • 顺序存储的二叉树

      顺序存储的二叉树通常只考虑完全二叉树

      存储的数组中,第n个元素的左子结点是2n+1,右子节点是2n+2,父节点是(n-1)/2。
      顺序存储的二叉树的遍历(把数组看成完全二叉树)

    package demo6;
    
    public class ArrayBinaryTree {
    
    	int[] data;
    	
    	public ArrayBinaryTree(int[] data) {
    		this.data=data;
    	}
    	
    	public void frontShow() {
    		frontShow(0);
    	}
    	
    	//前序遍历
    	public void frontShow(int index) {
    		if(data==null||data.length==0) {
    			return;
    		}
    		//先遍历当前节点的内容
    		System.out.println(data[index]);
    		//2*index+1:处理左子树
    		if(2*index+1<data.length) {
    			frontShow(2*index+1);
    		}
    		//2*index+2:处理右子树
    		if(2*index+2<data.length) {
    			frontShow(2*index+2);
    		}
    	}
    	
    }
    
    package demo6;
    
    public class TestArrayBinaryTree {
    
    	public static void main(String[] args) {
    		int[] data = new int[] {1,2,3,4,5,6,7};
    		ArrayBinaryTree tree = new ArrayBinaryTree(data);
    		//前序遍历
    		tree.frontShow();
    	}
    
    }
    
  • 相关阅读:
    selet 语句详解
    第三章 sql 的约束
    第二章 创建数据库并插入数据
    第一章
    微信小程序(九)
    微信小程序(七)
    微信小程序(八)
    微信小程序(六)
    bzoj4622 [NOI 2003] 智破连环阵
    bzoj3996 [TJOI2015]线性代数
  • 原文地址:https://www.cnblogs.com/lihao-bupt/p/13036351.html
Copyright © 2011-2022 走看看