二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
源代码:
- /**
- *
- * @author sunnyykn
- */
- import java.io.*;
- import java.util.*;
- class Node
- {
- public int iData; //data item (key)
- public double dData; //data item
- public Node leftChild; //this node's left child
- public Node rightChild; //this node's right child
- public void displayNode() //display ourself
- {
- System.out.print("{");
- System.out.print(iData);
- System.out.print(",");
- System.out.print(dData);
- System.out.print("}");
- }
- }//end class Node
- class Tree
- {
- private Node root; //first node of tree
- public Tree()
- {
- root = null; //no nodes in tree yet
- }
- public Node find(int key) //find node with given key
- {
- Node current = root; //start at root
- while(current.iData != key) //while no match
- {
- if(key < current.iData) //go left?
- current = current.leftChild;
- else //go right?
- current = current.rightChild;
- if (current == null) //didn't find it
- return null;
- }
- return current;
- }//end find()
- public void insert(int id ,double dd)
- {
- Node newNode = new Node(); //make new node
- newNode.iData = id; //insert data
- newNode.dData = dd;
- if( root == null ) //no node in root
- root = newNode;
- else
- {
- Node current = root; //start at root
- Node parent;
- while( true )
- {
- parent = current;
- if(id < current.iData) //go left?
- {
- current = current.leftChild;
- if(current == null) //if end of the line
- {
- parent.leftChild = newNode;
- return ;
- }
- }//end if go left
- else //or go right
- {
- current = current.rightChild;
- if(current == null) //if end of the line
- {
- parent.rightChild = newNode;
- return ;
- }
- }//end else go right
- }//end while
- }//end else not root
- }//end insert()
- public boolean delete(int key) //delete node with given key
- {
- Node current = root;
- Node parent = root;
- boolean isLeftChild = true;
- while(current.iData != key)
- {
- parent = current;
- if(key < current.iData)
- {
- isLeftChild = true;
- current = current.leftChild;
- }
- else
- {
- isLeftChild = false;
- current = current.rightChild;
- }
- if(current == null)
- return false;
- }//end while
- //found node to delete
- //if no children , simply delete it
- if(current.leftChild == null && current.rightChild == null)
- {
- if (current == root)
- root = null;
- else if(isLeftChild)
- parent.leftChild = null;
- else
- parent.rightChild = null;
- }
- //if no right child , replace with left subtree
- else if(current.rightChild == null)
- {
- if (current == root)
- root = current.leftChild;
- else if (isLeftChild)
- parent.leftChild = current.leftChild;
- else
- parent.rightChild = current.leftChild;
- }
- //if no left child , replace with right subtree
- else if (current.leftChild == null)
- {
- if (current == root)
- root = current.rightChild;
- else if(isLeftChild)
- parent.leftChild = current.rightChild;
- else
- parent.rightChild = current.rightChild;
- }
- //two children,so replace with inorder successor
- else
- {
- //get successor of node to delete(current)
- Node successor = getSuccessor(current);
- //connect parent of current to successor insteed
- if(current == root)
- root = successor;
- else if (isLeftChild)
- parent.leftChild = successor;
- else
- parent.rightChild = successor;
- //connect successor to current's left child
- successor.leftChild = current.leftChild;
- }//end else two children
- //(successor cannot have a left child)
- return true;
- }//end delete()
- //return node with next-highest value after delNode
- //goes to right child , then right child's left descendents
- private Node getSuccessor(Node delNode)
- {
- Node successorParent = delNode;
- Node successor = delNode;
- Node current = delNode.rightChild; //go to right child
- while(current != null) //until no more left children
- {
- successorParent = successor;
- successor = current;
- current = current.leftChild; //go to left child
- }
- if (successor != delNode.rightChild) //if successor not right child , make connections
- {
- successorParent.leftChild = successor.rightChild;
- successor.rightChild = delNode.rightChild;
- }
- return successor;
- }
- public void traverse(int traverseType)
- {
- switch(traverseType)
- {
- case 1:
- System.out.print("/nPreorder traversal:");
- preOrder(root);
- break;
- case 2:
- System.out.print("/nInorder traversal: ");
- inOrder(root);
- break;
- case 3:
- System.out.print("/nPostorder traversal:");
- postOrder(root);
- break;
- }
- System.out.println("");
- }
- private void preOrder(Node localRoot)
- {
- if (localRoot != null)
- {
- System.out.print(localRoot.iData + " ");
- preOrder(localRoot.leftChild);
- preOrder(localRoot.rightChild);
- }
- }
- private void inOrder(Node localRoot)
- {
- if(localRoot != null)
- {
- inOrder(localRoot.leftChild);
- System.out.print(localRoot.iData + " ");
- inOrder(localRoot.rightChild);
- }
- }
- private void postOrder(Node localRoot)
- {
- if (localRoot != null)
- {
- postOrder(localRoot.leftChild);
- postOrder(localRoot.rightChild);
- System.out.print(localRoot.iData + " ");
- }
- }
- public void displayTree()
- {
- Stack globalStack = new Stack();
- globalStack.push(root);
- int nBlanks = 32;
- boolean isRowEmpty = false ;
- System.out.println("....................................................");
- while(isRowEmpty == false)
- {
- Stack localStack = new Stack();
- isRowEmpty = true;
- for(int j = 0;j < nBlanks; j ++)
- System.out.print(" ");
- while(globalStack.isEmpty() == false)
- {
- Node temp = (Node)globalStack.pop();
- if(temp != null)
- {
- System.out.print(temp.iData);
- localStack.push(temp.leftChild);
- localStack.push(temp.rightChild);
- if(temp.leftChild != null || temp.rightChild != null)
- isRowEmpty = false;
- }
- else
- {
- System.out.print("--");
- localStack.push(null);
- localStack.push(null);
- }
- for(int j = 0; j < nBlanks*2 - 2;j ++)
- System.out.print(" ");
- }//end while globalStack not empty
- System.out.println("");
- nBlanks /= 2;
- while(localStack.isEmpty() == false)
- globalStack.push( localStack.pop() );
- }//end while isRowEmpty is false
- System.out.println("....................................................");
- }//end displayTree()
- }//end class Tree
- class TreeApp
- {
- public static void main(String[] args) throws IOException
- {
- int value ;
- Tree theTree = new Tree();
- theTree.insert(50, 1.5);
- theTree.insert(25, 1.2);
- theTree.insert(75, 1.7);
- theTree.insert(12, 1.5);
- theTree.insert(37, 1.2);
- theTree.insert(43, 1.7);
- theTree.insert(30, 1.5);
- theTree.insert(33, 1.2);
- theTree.insert(87, 1.7);
- theTree.insert(93, 1.5);
- theTree.insert(97, 1.5);
- while( true )
- {
- System.out.print("Enter first letter of show,insert,find,delete,or traverse:");
- int choice = getChar();
- switch( choice )
- {
- case 's':
- theTree.displayTree();
- break;
- case 'i':
- System.out.print("Enter value to insert:");
- value = getInt();
- theTree.insert(value, value + 0.9);
- break;
- case 'f':
- System.out.print("Enter value to find:");
- value = getInt();
- Node found = theTree.find(value);
- if(found != null)
- {
- System.out.print("Found:");
- found.displayNode();
- System.out.print("/n");
- }
- else
- {
- System.out.print("Could not find ");
- System.out.print(value + '/n');
- }
- break;
- case 'd':
- System.out.print("Enter value to delete:");
- value = getInt();
- boolean didDelete = theTree.delete(value);
- if(didDelete)
- System.out.print("Deleted " + value + '/n');
- else{
- System.out.print("Could not delete ");
- System.out.print(value + '/n');
- }
- break;
- case 't':
- System.out.print("Enter type 1,2 or 3:");
- value = getInt();
- theTree.traverse(value);
- break;
- default:
- System.out.print("Invalid entry/n");
- }//end switch
- }//end while
- }//end main()
- public static String getString() throws IOException
- {
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(isr);
- String s = br.readLine();
- return s;
- }
- public static char getChar() throws IOException
- {
- String s = getString();
- return s.charAt(0);
- }
- public static int getInt() throws IOException
- {
- String s = getString();
- return Integer.parseInt(s);
- }
- }//end class TreeApp
二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法相关推荐
- 二叉树的前、中、后序遍历的代码实现(递归方式)
测试的二叉树的结构 root lfb1 rtb1rtb2 控制台输出的遍历结果 ======从根节点开始,前序遍历此二叉树======= root lfb1 rtb1 rtb2 ======从根节点开 ...
- 二叉树的前、中、后序遍历
所谓二叉树遍历是按某种特定规则,依次对二叉树中的节点进行相应的操作,并且每个节点只操作一次.访问结点所做的操作依赖于具体的应用问题. 遍历是二叉树上最重要的运算之一,也是二叉树进行其它运算的基础. 二 ...
- 【数据结构与算法】力扣:二叉树的前、中、后序遍历
递归法 前序遍历 给你二叉树的根节点 root ,返回它节点值的前序 遍历. 示例 1: 输入:root = [1,null,2,3] 输出:[1,2,3] 示例 2: 输入:root = [] 输出 ...
- java中二叉树_Java工程师面试1000题224-递归非递归实现二叉树前、中、后序遍历...
224.使用递归和非递归实现二叉树的前.中.后序遍历 使用递归来实现二叉树的前.中.后序遍历比较简单,直接给出代码,我们重点讨论非递归的实现. class Node { public int valu ...
- 二叉树遍历方法——前、中、后序遍历(图解)
目录 一.前序遍历 (1)递归版本 (2)非递归版本 二.中序遍历 (1)递归版本 (2)非递归版本 三.后序遍历 (1)递归版本 (2)非递归版本 四.总结 五.测试程序 六.程序输出 二叉树的遍历 ...
- 二叉树的前、中、后的非递归遍历
题目 实现一个链式存储的二叉树,采用非递归的形式,按照前.中.后序的顺序遍历二叉树. 代码 /** * 二叉树的前.中.后序的非递归遍历 **/#include <iostream> us ...
- 先序序列和中序序列构造二叉树,中序序列和后序序列构造二叉树
1:首先读者要了解二叉树BinaryTree基本概念,其次区分左子树与左孩子节点,右子树与右孩子节点.(在数据结构中 一个节点可以成为一棵树,对于没有孩子节点的节点称为为叶子节点). 2:在 ...
- 已知一棵二叉树的中序序列和后序序列,写一个建立该二叉树的二叉链表存储结构的算法...
已知一棵二叉树的中序序列和后序序列,写一个建立该二叉树的二叉链表存储结构的算法 #define N 10 //二叉树节点的个数 char postorderstr[]={};//后序序列 char i ...
- 7-10 先序序列创建二叉树,输出先序序列、中序序列、后序序列并输出叶子结点数 (10 分)
7-10 先序序列创建二叉树,输出先序序列.中序序列.后序序列并输出叶子结点数 (10 分) 对于给定的二叉树,输出其先序序列.中序序列.后序序列并输出叶子结点数. 输入格式: 二叉树的先序遍历序列. ...
最新文章
- android ffmpeg 编码h264,Mac系统下ffmpeg+h264+flv编码的android录制屏幕实现2
- Sinew Ex深度发掘金融衍生品市场价值
- elementui table html,elementUI Table表格表头自定义
- window.onload 不执行
- 在linux系统下安装两个nginx以及启动、停止、重起
- 编译linux内核成vmlinuz,编译一个内核 - no bzImage/vmlinuz生成
- 16行代码AC_【第十届蓝桥杯省赛c/c++B组真题解析】7.完全二叉树的权值
- different behavior dialog popup display no
- 如何批量查询PR值、百度权重、百度快照及收录量,用BlueCatTools批量网站查询工具
- Java之WeakReference与SoftReference使用讲解
- mysql按照列构建索引_列存储索引增强功能–在线和离线(重新)构建
- 单片机c语言程序包txt,单片机C语言应用100例(第3版)(含光盘1张) pdf epub mobi txt 下载...
- 悼念前端大牛司徒正美
- Android限制录制屏幕无声音,哪一个安卓录屏软件可以录制系统的声音
- 如何最大效率压缩视频文件(ffmpeg)
- 阿里电商故障治理和故障演练实践
- 【托业】【跨栏】TEST04
- 设备管理器中的非即插即用驱动程序
- cass实体编码列表
- hdu1404 博弈